Jump to content

Recommended Posts

I've built a image hosting script for a client and am seeing an oddity that I am hoping you all can help me with.

 

I've built a page (index.php) where a person can upload an image.  I take the image, store it, and put some info on it into a DB.  A piece of that info is a url string that points to the image they uploaded.  I grab this info and put it into a $_SESSION variable ala:

 

$_SESSION['image_url'] = $image_share['url'];

 

I then scoot them on to the next page (display.php) where it's suppose to show a thumbnail and links to their uploaded image.  I use the $_SESSION variable mentioned above to display the thumb/links akin to:

 

<img src="thumbs/<?php echo $_SESSION['image_url'];?>" alt="" />

 

The Problem...

is that the first time an image is uploaded during a session, the image gets uploaded and the info is inserted into the DB just fine but the thumb and links don't show the $_SESSION variable on display.php.  If I go back to index.php and try a to upload any image after this, the thumb and links show up just fine.

 

Any idea on why this may be happening?

Link to comment
https://forums.phpfreaks.com/topic/192983-session-question/
Share on other sites

How, exactly, do you "scoot" the user to the display page? Did you check the HTML source code when the images don't load to see if there is a value for the image path?

 

On index.php, right after setting the session variable, I use:

 

header ('location: http://www.clientsite.com/display.php');

 

 

Probably a dumb question, but I'll ask.

 

Are you using session_start(); ?

 

That's not a dumb question at all, but yes I have session_start(); right after the opening <?php tag.

 

Link to comment
https://forums.phpfreaks.com/topic/192983-session-question/#findComment-1016381
Share on other sites

Probably a dumb question, but I'll ask.

 

Are you using session_start(); ?

 

That's not a dumb question at all, but yes I have session_start(); right after the opening <?php tag.

 

 

At the risk of beating a dead horse, are you using session_start() on index.php and again when you do the redirect to display.php? I did some local tests and that is the only thing I can come up with to replicate what you are seeing. Once you use the header funtion to redirect to another page, pretty much all "state" values are lost, session, POST, GET, etc.

Link to comment
https://forums.phpfreaks.com/topic/192983-session-question/#findComment-1016410
Share on other sites

At the risk of beating a dead horse, are you using session_start() on index.php and again when you do the redirect to display.php?

 

I have session_start() at the very top of both index.php and display.php if that's what you mean.  Or are you saying that I should also have it posted right before/after the header redirect?

 

If all the info is lost on a redirect, how is it that it's retaining the info each time after the first try?  I'm only losing the session info on the first try.

Link to comment
https://forums.phpfreaks.com/topic/192983-session-question/#findComment-1016417
Share on other sites

Here's a couple more quandaries as well. When do you run:

 

$_SESSION['image_url'] = $image_share['url'];

 

When is $image_share['url'] defined? Depending on how it's defined, you may have to set it after the database insert. You're also going to need to define $_SESSION['image_url'] after you've defined the $image_share['url'] variable.

 

The timing of all this might be important. From the code you've provided it's hard to tell when these things are happening.

Link to comment
https://forums.phpfreaks.com/topic/192983-session-question/#findComment-1016419
Share on other sites

My apologies for being so vague with the code but since this is for a client and it will be his code, I always ask to show it and sometimes they restrict me in what I can show.

 

Here's the bits that covers the DB insertion, variable assignment and redirecting them.  Hopefully this will help shed some light. 

NOTE: $filename and $ip_ad variables are defined early on.

 

// Insert the images' info into the database //
mysql_query ("INSERT INTO images SET `url`='$filename',
							`created`=now(),
							`ip_ad`='$ip_ad',
							`views`='0'");

// Grab the info we'll need to post the thumb and codes on display.php //
$ii_qry = "SELECT * FROM images WHERE url = '$filename'";
$ii_res = mysql_query($ii_qry);
$image_share = mysql_fetch_array($ii_res);

$_SESSION['image_url'] = $image_share['url'];


// Move them along to the display page //
header ('location: http://www.clientsite.com/display.php');
exit;

Link to comment
https://forums.phpfreaks.com/topic/192983-session-question/#findComment-1016440
Share on other sites

I can't spend forever on this project so I decided to go with an alternative solution.  I decided to do away with the session idea altogether and redo the header redirect to:

 

header ('location: http://www.clientsite.com/display.php?i='.$filename);

 

and use _GET on display.php to get the info I need.

 

Thanks for all the help though!

Link to comment
https://forums.phpfreaks.com/topic/192983-session-question/#findComment-1016495
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.