Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pass Data from One PHP Page to Another using header()

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.

like image 867
user1059869 Avatar asked Jul 12 '26 22:07

user1059869


1 Answers

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!

like image 81
Andrew Kozak Avatar answered Jul 15 '26 11:07

Andrew Kozak



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!