missyevil Posted March 23, 2006 Share Posted March 23, 2006 Firstly, sorry for posting about sessions - I know there are lots already but I *have* searched, and asked friends, but can't figure this one out!!I have a simple login system up and running, using PHP and MySQL. Data is being retrieved from the database ok, as the user can login. The problem is that the $_SESSION variables I set when they login are not being displayed in the next page, even though I am using session_start(); at the beginning of each page.Any help would be much appreciated as this has had me stumpped for days!!Here's the code:login.php[code]<? session_start();include 'db.php';// Convert to simple variables$email = $_POST['email'];$password = $_POST['password'];if((!$email) || (!$password)){ echo "<strong>Please enter ALL of the information! </strong><br />"; include 'welcome.htm'; exit();}// check if the user info validates the db$sql = "SELECT * FROM jslogin WHERE email='$email' AND password='$password'";$qresult=mysql_query($sql);$login_check = mysql_num_rows($qresult);if($login_check > 0){ while($row = mysql_fetch_array($qresult)) { foreach( $row AS $key => $val ) { $$key = stripslashes( $val ); } $_SESSION[valid] = $valid; // Register some session variables! session_register('jsid'); $_SESSION['jsid'] = $jsid; session_register('forename'); $_SESSION['forename'] = $forename; session_register('surname'); $_SESSION['surname'] = $surname; session_register('email'); $_SESSION['email'] = $email; mysql_query("UPDATE jslogin SET last_jslogin=now() WHERE jsid='$jsid'"); header("Location: jshome.php"); }}else{ echo "<strong>You could not be logged in!<br /> Please try again!</strong><br />"; include 'welcome.htm';}?>[/code]jshome.php[code]<?session_start();echo "Welcome ". $_SESSION['forename'] ." ". $_SESSION['surname'] ."! You are logged in!<br /><br />";echo "<br /><a href=logout.php>Logout</a>";?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/5602-session-variables/ Share on other sites More sharing options...
ober Posted March 23, 2006 Share Posted March 23, 2006 You don't need to use session_register().Are you getting any errors? Is there any whitespace before you open the PHP tags before session_start()? Quote Link to comment https://forums.phpfreaks.com/topic/5602-session-variables/#findComment-19986 Share on other sites More sharing options...
kenrbnsn Posted March 23, 2006 Share Posted March 23, 2006 In each script put this debugging line after the "session_start()" line:[code]<?php echo '<pre>' . print_r($_SESSION,true) . '</pre>'; ?>[/code]This will dump the contents of the $_SESSION array to your screen so you can see what the system thinks you stored.Also, what web server are you using? I have heard of problems with sessions and IIS.Ken Quote Link to comment https://forums.phpfreaks.com/topic/5602-session-variables/#findComment-19990 Share on other sites More sharing options...
missyevil Posted March 23, 2006 Author Share Posted March 23, 2006 No - no whitespace.I wasn't getting any errors before. Now when I add the code suggested it shows the contents of the array, which has the jsid (primary key) and the email address in it....And I also get this error now:Warning: Cannot modify header information - headers already sent by (output started at C:\Program Files\Apache Group\Apache2\htdocs\Project\login.php:3) in C:\Program Files\Apache Group\Apache2\htdocs\Project\login.php on line 42I'm using Apache as my webserver. Quote Link to comment https://forums.phpfreaks.com/topic/5602-session-variables/#findComment-20010 Share on other sites More sharing options...
ober Posted March 23, 2006 Share Posted March 23, 2006 You would get that if you output something to the browser before calling the header() function.One other thing I noticed:$_SESSION[valid] = $valid;You don't have any quotes around the key name.$_SESSION['valid'] = $valid; Quote Link to comment https://forums.phpfreaks.com/topic/5602-session-variables/#findComment-20013 Share on other sites More sharing options...
missyevil Posted March 23, 2006 Author Share Posted March 23, 2006 Have finally realised my mistake (and my own stupidity) - the login variables (email and password) are stored in a different table in my database that the other variables.Can anyone suggest an easy way to get variables from other tables at the same time as a user is logging in, and assign them to $_SESSION variables?? Quote Link to comment https://forums.phpfreaks.com/topic/5602-session-variables/#findComment-20030 Share on other sites More sharing options...
ober Posted March 23, 2006 Share Posted March 23, 2006 Run a query to get them.....? You can run more than one query on the page.I'm confused by the question. Quote Link to comment https://forums.phpfreaks.com/topic/5602-session-variables/#findComment-20049 Share on other sites More sharing options...
keeB Posted March 23, 2006 Share Posted March 23, 2006 [!--quoteo(post=357680:date=Mar 23 2006, 05:31 PM:name=missy)--][div class=\'quotetop\']QUOTE(missy @ Mar 23 2006, 05:31 PM) [snapback]357680[/snapback][/div][div class=\'quotemain\'][!--quotec--]Have finally realised my mistake (and my own stupidity) - the login variables (email and password) are stored in a different table in my database that the other variables.Can anyone suggest an easy way to get variables from other tables at the same time as a user is logging in, and assign them to $_SESSION variables??[/quote]Of course.. You can use a JOIN.. or you can use a simple statement[!--sql--][div class=\'sqltop\']SQL[/div][div class=\'sqlmain\'][!--sql1--][span style=\'color:blue;font-weight:bold\']SELECT[/span] * [color=green]FROM[/color] [color=orange]jslogin,[/color] otherTable [color=green]WHERE[/color] jslogin.email [color=orange]=[/color] [color=red]'some email'[/color] [color=blue]AND[/color] jslogin.password [color=orange]=[/color] [color=red]'some password'[/color] [color=blue]AND[/color] [!--sql2--][/div][!--sql3--]Notice the ', otherTable' obviously that's the other table where the data is stored.. I can't help anymore without knowing your db structure, but you get the hint. :D Quote Link to comment https://forums.phpfreaks.com/topic/5602-session-variables/#findComment-20057 Share on other sites More sharing options...
missyevil Posted March 24, 2006 Author Share Posted March 24, 2006 Thanks!! I'll give it a go :) Quote Link to comment https://forums.phpfreaks.com/topic/5602-session-variables/#findComment-20259 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.