maudise Posted December 10, 2012 Share Posted December 10, 2012 Hi all, I'm at a bit of a loss I'm afraid, I'm pretty new to PHP and have been looking to code a simple log in page connecting to a mysql database. I'm running firefox 16.0.2 and have my websites hosted with hostgator.com. I've created the log in forms without too much trouble but what I can't seem to get is the $_Session variables to do keep the variables between pages. In an attempt to isolate the problem I have stripped the issue right back to just passing the variables between two pages; sessionstart.php and sessioncheck.php the code in sessionstart.php is simple enough; At the very top of my page I have the following code <?php session_start(); $_Session['test'] = 'this is a Session variable'; ?> and then as a test I put in two sections of code in the body of the HTML tags <?php $Teststring = "this is a test string"; print $Teststring; ?> <?php print $_Session['test']; ?> I know the separate php tags aren't required but I was trying to play around with things. Both of those prints work fine and not a problem. On sessioncheck.php I have the following code at the very top of my page <?php session_start(); echo $_Session['$test']; echo 'I tried'; ?> sessioncheck.php doesn't load the session variables at all, also the switching between print and echo was simply done to ensure that that wasn't the problem at all. I've done a fair amount of searching around and to pre-empt a few questions which might be queried I have the following answers; Session_ID(); returns the same Session ID the phpinfo file tells me the php.ini files are loaded successfully I can't remember the third but the other option didn't work either. I've got to the end of my very limited knowledge so if anyone could give me any advice I would greatly appreciate it! Regards Maudise Quote Link to comment https://forums.phpfreaks.com/topic/271835-session-variables-not-passing/ Share on other sites More sharing options...
lukep11a Posted December 10, 2012 Share Posted December 10, 2012 have u tried removing the $ sign from the session variable in sessioncheck.php so instead of echo $_Session['$test']; use echo $_Session['test']; Quote Link to comment https://forums.phpfreaks.com/topic/271835-session-variables-not-passing/#findComment-1398600 Share on other sites More sharing options...
MDCode Posted December 10, 2012 Share Posted December 10, 2012 (edited) $_SESSION variables are case-sensitive and need to be all uppercase Edited December 10, 2012 by SocialCloud Quote Link to comment https://forums.phpfreaks.com/topic/271835-session-variables-not-passing/#findComment-1398603 Share on other sites More sharing options...
Christian F. Posted December 11, 2012 Share Posted December 11, 2012 (edited) A slight correction on the wording, to avoid any confusion: The name of the $_SESSION superglobal is case-sensitive, as with all variable names in PHP, and thus needs to be in upper-case. As that's how PHP has defined it. The indices are also case-sensitive, but they can be set to either lower, upper or a mix of cases as you see fit. Edited December 11, 2012 by Christian F. Quote Link to comment https://forums.phpfreaks.com/topic/271835-session-variables-not-passing/#findComment-1398702 Share on other sites More sharing options...
maudise Posted December 12, 2012 Author Share Posted December 12, 2012 Hi, I had noticed the use of the $ and I have now changed to $_SESSION interestingly it has changed how it is displayed in the design view of dream weaver from a box which says PHP to {session.test}. However this hasn't solved my problem it still wont pass variables between the two pages. Thanks for the help so far Quote Link to comment https://forums.phpfreaks.com/topic/271835-session-variables-not-passing/#findComment-1399039 Share on other sites More sharing options...
MDCode Posted December 13, 2012 Share Posted December 13, 2012 (edited) How are you setting them and calling them? Full page of php code would help. Also, are you saying you are using dreamweaver to use php...? In which case that is your error. Dreamweaver can not parse php. Edited December 13, 2012 by SocialCloud Quote Link to comment https://forums.phpfreaks.com/topic/271835-session-variables-not-passing/#findComment-1399051 Share on other sites More sharing options...
maudise Posted December 14, 2012 Author Share Posted December 14, 2012 Hi, I'm currently out of the office so cannot upload the full file but I'll do it in a few hours when I'm back. What I'm doing is writing the code and uploading to my webpage to test whether it works. There are probably more efficient ways of doing it out there. So it's not actually DreamWeaver which is doing the parsing, but it's the webhost (hostgator.com) Maudise Quote Link to comment https://forums.phpfreaks.com/topic/271835-session-variables-not-passing/#findComment-1399427 Share on other sites More sharing options...
maudise Posted December 15, 2012 Author Share Posted December 15, 2012 Hi, The sessionstart.php page is below, and the session check in the code box below that. <?php session_start(); $_SESSION['test'] = 'this is a Session variable'; ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Untitled Document</title> </head> <body> I'm confused <?php $Teststring = "this is a test string"; print $Teststring; echo $_SESSION['test']; ?> </br> </p> Why is this not working! <?php print $_SESSION['test']; ?> </body> </html> And now for the Sessioncheck page <?php session_start(); echo $_SESSION['test']; echo 'I tried'; ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Untitled Document</title> </head> <body> <?php echo $_SESSION['test']; ?> Proof updated again </body> </html> Thanks Maudise Quote Link to comment https://forums.phpfreaks.com/topic/271835-session-variables-not-passing/#findComment-1399521 Share on other sites More sharing options...
Christian F. Posted December 15, 2012 Share Posted December 15, 2012 Do you have a link to the page, so that we can see this in effect ourselves? The only things I can think of right now, is that the cookie isn't being set properly. Or that you might be doing things out of order, though unlikely. In any case, do make sure that you're not blocking the cookie, and check to see if it is indeed being sent with the request from the browser. Quote Link to comment https://forums.phpfreaks.com/topic/271835-session-variables-not-passing/#findComment-1399522 Share on other sites More sharing options...
maudise Posted December 15, 2012 Author Share Posted December 15, 2012 sure, www.crosstrendanalysis.co.uk/testing/sessionstart.php and www.crosstrendanalysis.co.uk/testing/sessioncheck.php Regards Maudise Quote Link to comment https://forums.phpfreaks.com/topic/271835-session-variables-not-passing/#findComment-1399549 Share on other sites More sharing options...
MDCode Posted December 15, 2012 Share Posted December 15, 2012 (edited) Try turning on error reporting? Edited December 15, 2012 by SocialCloud Quote Link to comment https://forums.phpfreaks.com/topic/271835-session-variables-not-passing/#findComment-1399550 Share on other sites More sharing options...
PFMaBiSmAd Posted December 15, 2012 Share Posted December 15, 2012 Do you have php's error_reporting set to E_ALL and display_errors set to ON so that any session related errors will be reported and displayed? You could have a session.save_path problem or a header error due to your file's character encoding containing the BOM (Byte Order Mark) characters. Quote Link to comment https://forums.phpfreaks.com/topic/271835-session-variables-not-passing/#findComment-1399551 Share on other sites More sharing options...
maudise Posted December 16, 2012 Author Share Posted December 16, 2012 Hi, I put the following code at the start, on line 2 for both pages. ini_set('display_errors',1); error_reporting(E_ALL); I now get Notice: Undefined index: test in /home/maudise/public_html/testing/sessioncheck.php on line 5 as an error. For what it's worth, if you need to look at the phpinfo file it can be found at www.crosstrendanalysis.co.uk/testing/phpinfo.php. Thanks for the continued help, I did look around for the undefined index problem but couldnt' find a solution which wasn't a 'Session_start()' problem or similar. Could it be a server side issue which Hostgator needs to deal with? Regards Maudise Quote Link to comment https://forums.phpfreaks.com/topic/271835-session-variables-not-passing/#findComment-1399714 Share on other sites More sharing options...
PFMaBiSmAd Posted December 16, 2012 Share Posted December 16, 2012 The output from your sessionstart.php file doesn't match the posted code above. Are you sure what's actually in that files? Could you post the sessionstart.php file again. Are you using any foreign language characters (non ASCII) or any control/non-printing in the 'test' array index name, so that what is actually showing in your file isn't what is being shown in your posts in the forum? We have seen times where the copy/paste into the forum post produces 'clean' code that works, but there is something in the actual code file that doesn't match. Try deleting the ['test'] portion of the $_SESSION variable and retype it. When I visit your two pages, I get the same session id cookie value on both pages. That indicates that the session_start on both pages is starting/resuming the same session data, but the ['test'] index isn't present on the 2nd page. There may be an issue with the server where the file name is being created, but the actual file isn't (there is typically a php error when this type of thing occurs.) Quote Link to comment https://forums.phpfreaks.com/topic/271835-session-variables-not-passing/#findComment-1399730 Share on other sites More sharing options...
maudise Posted December 17, 2012 Author Share Posted December 17, 2012 well, I genuinely have no idea why this is working now. I've gone back and changed the ['test'] to be ["test"] on all the variables and it seems to now be working. Thank you very much for your help, if you have any great words of wisdom as to how to avoid this in the future [' '] wasn't picked up and it was copied from a tutorial, so I'm presuming you can use a single quotation on some systems? Regards Maudise Quote Link to comment https://forums.phpfreaks.com/topic/271835-session-variables-not-passing/#findComment-1399928 Share on other sites More sharing options...
Christian F. Posted December 17, 2012 Share Posted December 17, 2012 Single quotes are available on all PHP installations, or rather should be. However, I suspect that the text you copied didn't actually contain single quotes. Just something that looked like single quotes, for us humans. Quote Link to comment https://forums.phpfreaks.com/topic/271835-session-variables-not-passing/#findComment-1399942 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.