3

I want to send data from one PHP file to another PHP file in a subfolder where the first PHP file is present. I have a folder named folder1 which has contains a PHP file named file1.php and I want to call another file named file2.php in a subfolder of folder1 named folder2. I am using the header() function like this in file1.php:

$host  = $_SERVER['HTTP_HOST'];
$uri   = rtrim(dirname($_SERVER['PHP_SELF']), '/\\');
$extra = 'folder1/folder2/file2.php';
header("location:http://$host$uri/$extra?sms=".$msg."&num=".$msg_num);

Data is not passing. Is there any solution using header()? I can't use cURL because of some restrictions.

8
  • 1
    Can you be more specific with what "data is not passing" means? Commented Nov 22, 2011 at 13:48
  • query string parameters are empty in file2.php. Even though they are being filled with valid data in file1.php Commented Nov 22, 2011 at 13:50
  • What do you get when you echo "location:http://$host$uri/$extra?sms=".$msg."&num=".$msg_num; Commented Nov 22, 2011 at 13:51
  • @jprofitt i am getting the data when i echo like that... but data does not get passed to file2 Commented Nov 22, 2011 at 13:59
  • I don't see you initializing $msg and $msg_num. As @jprofitt said, basically debugging 101, print the result and see if it matches your expectations. The only possibility here is that those variables aren't set and the query string is loaded with empty variables. Or, but you didn't say so, you've already echo'd something and you get the warning that headers are already sent, and the redirect never takes place. How do you see that the "data does not get passed to file2"? Have you tried var_dump($_SERVER);? Commented Nov 22, 2011 at 14:00

2 Answers 2

4

The following code works:

file1.php:

<?php

header( 'Location: inner/file2.php?x=1&y=2&z=3' );

?>

inner/file2.php:

<?php

print '<pre>';

var_dump( $_GET );

print '</pre>';

?>

The result of visiting http://localhost/testing/file1.php is a redirect to http://localhost/testing/inner/file2.php?x=1&y=2&z=3 which displays:

array(3) {
  ["x"]=>
  string(1) "1"
  ["y"]=>
  string(1) "2"
  ["z"]=>
  string(1) "3"
}

I would suggest copying over these test files and proving to yourself that the basic concept of redirecting with passed values is working. Then, build up the rest of your code around a known-good kernel. Good luck!

Sign up to request clarification or add additional context in comments.

Comments

1

Without an example of the variables you want to send, it's kind of hard to tell what the problem might be, but a possible problem could be the characters in the variables.

To make sure there are no invalid characters, you can use urlencode(), perhaps in combination with htmlentities(), see the manual:

header("location:http://$host$uri/$extra?sms=".urlencode($msg)."&num=".urlencode($msg_num));

2 Comments

Also, you need to make sure that file2.php is in fact looking for the correct parameters.
i want to pass sms and num to file2.php from file1.php... i am assigning them correctly and taking the correct parameters using $_get in file2.php... but there is no data in the query string itself

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.