86Stang Posted February 22, 2010 Share Posted February 22, 2010 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? Quote Link to comment https://forums.phpfreaks.com/topic/192983-session-question/ Share on other sites More sharing options...
Psycho Posted February 22, 2010 Share Posted February 22, 2010 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? Quote Link to comment https://forums.phpfreaks.com/topic/192983-session-question/#findComment-1016338 Share on other sites More sharing options...
seventheyejosh Posted February 22, 2010 Share Posted February 22, 2010 Probably a dumb question, but I'll ask. Are you using session_start(); ? Quote Link to comment https://forums.phpfreaks.com/topic/192983-session-question/#findComment-1016341 Share on other sites More sharing options...
86Stang Posted February 22, 2010 Author Share Posted February 22, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/192983-session-question/#findComment-1016381 Share on other sites More sharing options...
86Stang Posted February 22, 2010 Author Share Posted February 22, 2010 Also, mjdamato, I did check the source and there is no value in the image path where the session variable should be. Quote Link to comment https://forums.phpfreaks.com/topic/192983-session-question/#findComment-1016382 Share on other sites More sharing options...
Psycho Posted February 22, 2010 Share Posted February 22, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/192983-session-question/#findComment-1016410 Share on other sites More sharing options...
86Stang Posted February 22, 2010 Author Share Posted February 22, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/192983-session-question/#findComment-1016417 Share on other sites More sharing options...
ialsoagree Posted February 22, 2010 Share Posted February 22, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/192983-session-question/#findComment-1016419 Share on other sites More sharing options...
86Stang Posted February 22, 2010 Author Share Posted February 22, 2010 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; Quote Link to comment https://forums.phpfreaks.com/topic/192983-session-question/#findComment-1016440 Share on other sites More sharing options...
ialsoagree Posted February 22, 2010 Share Posted February 22, 2010 The timing of everything looks good. I have to say, I feel stumped. It could be a problem with the browser page getting cached (and hitting refresh tells it to update the cached page)... Quote Link to comment https://forums.phpfreaks.com/topic/192983-session-question/#findComment-1016447 Share on other sites More sharing options...
86Stang Posted February 23, 2010 Author Share Posted February 23, 2010 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! Quote Link to comment https://forums.phpfreaks.com/topic/192983-session-question/#findComment-1016495 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.