1

I'm having a problem passing a variable from one PHP file to another PHP file. I'm trying to send the varialbe $insert to another PHP file that will just display it. For the life of me, I can't figure out why it won't work.

Any help would be great! Thanks.

1st PHP File (Sending $insert to $_SESSION['finalimage'])

<?php

 session_start();
 $insert = rand(5, 1500);
 $_SESSION['finalimage'] = $insert;

 header("Location: http://www.shmoggo.com/snapshot/snapshot_view.php");

 echo base64_decode($_POST["image"]);

 $final = base64_decode($_POST["image"]);


 $newpath = "uploads/" . $insert . ".jpg"; 
 file_put_contents($newpath, $final);

 ?>

2nd PHP File (Receiving $insert from $_SESSION['finalimage'])

 <?php

 session_start();
 $insert = $_SESSION['finalimage'];
 echo "Image Number = ". $insert;

 ?>
12
  • 3
    What do you mean by "won't work", exactly? Commented Jan 9, 2012 at 21:48
  • 1
    What happens or doesn't happen? Commented Jan 9, 2012 at 21:48
  • 4
    Are both of these pages on the same domain? Commented Jan 9, 2012 at 21:48
  • 6
    why are you executing any code after redirection? You should always exit() after header("Location: Commented Jan 9, 2012 at 21:48
  • 3
    Might want to verify that the session ID (get it via session_id()) is the same on both pages. If it's not, you've got at least 2 different sessions in use, and the one on your 2nd page is empty/new. Commented Jan 9, 2012 at 21:49

1 Answer 1

1

First thing to check is how your PHP installation is supporting sessions. There are two possible methods for "persisting" the session ID between requests:

1) using cookies or 2) using "URL rewriting" (ie each URL passes a sessionid value)

If you are using cookies check to see if your server is in fact sending a cookie to your browser.

If you are using url-rewriting the Location header that you are sending that re-routes the client browser to "snapshot_view.php" will need to be modified to include the session id. Like this:

$reroute = 'Location: http://www.shmoggo.com/snapshot/snapshot_view.php?PHPSESSID=' . session_id();
header($reroute);
Sign up to request clarification or add additional context in comments.

2 Comments

This is what I needed, thank you. FireFox isn't enabling cookies to allow the cookie method to work. I can't figure out how to use this code you gave me to do that same thing with cookies... any wise words?
Hi Aaron -- if you were using a cookie to persist the session ID at the browser then the above code would not be necessary because the cookie is automatically sent in with each request. URL rewriting does not depend on cookie support but it means that every URL must include the PHPSESSID value - PHP will look after most of these for you automatically but when sending out a "Location" header you must do this manually. PS in your original code you should probably move the "Header()" call to the end of the code to avoid get back a request before the image file has been committed to disk.

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.