dachshund Posted September 4, 2009 Share Posted September 4, 2009 for some reason as soon as i log (which works fine) the session closes and it says i am no longer logged in. here's my log in form <?php if(isset($_SESSION['uid'])){ $sessionname = $_SESSION['uid']; $accountsql = "SELECT * FROM users WHERE `userid` = '$sessionname'"; $accountres = mysql_query($accountsql) or die(mysql_error()); $accountrow = mysql_fetch_assoc($accountres); echo 'Hello '; echo $accountrow['username']; echo ' ( '; echo $accountrow['email']; echo ' ).'; echo '<div id="profilepicture">'; echo '<img src="/images/user/'; echo $accountrow['userid']; echo '/profilepicture.jpg" width="60" height="60">'; echo '</div>'; echo 'Upload a new icon'; }else { if(!$_POST['login']){ echo '<form name="login" method="post" action="#">'; echo '<p>Username: <input type="text" name="username"></p>'; echo '<p>Password: <input type="password" name="password"></p>'; echo '<p><input type="submit" name="login" value="Submit"></p>'; echo '</form>'; }else { $username = $_POST['username']; $password = $_POST['password']; if($username && $password){ $sql = "SELECT * FROM users WHERE `username`='$username'"; $res = mysql_query($sql) or die(mysql_error()); if(mysql_num_rows($res) == 1){ $epass = md5($password); $sql2 = "SELECT * FROM users WHERE `username` = '$username' AND `password` = '$epass'"; $res2 = mysql_query($sql2) or die(mysql_error()); if(mysql_num_rows($res2) ==1){ //success $rows = mysql_fetch_assoc($res2); $_SESSION['uid'] = $rows['userid']; echo 'You are logged in as '; echo $username; }else { echo 'Wrong username and/or password'; } }else { echo 'Username does not exist.'; } }else { echo 'Please fill in all fields.'; } } } ?> i have session_start() in the header of every page etc etc. this used to be working fine but now it doesn't work at all and I don't know what's changed. when i put this in the header of the log in page ini_set("display_errors", "1"); error_reporting(E_ALL); it says this before i log in NOTICE: UNDEFINED INDEX: UID any help would be great Quote Link to comment https://forums.phpfreaks.com/topic/173151-solved-_sessionuid-wont-stay-open/ Share on other sites More sharing options...
Daniel0 Posted September 4, 2009 Share Posted September 4, 2009 I don't see session_start() anywhere on that page... Quote Link to comment https://forums.phpfreaks.com/topic/173151-solved-_sessionuid-wont-stay-open/#findComment-912651 Share on other sites More sharing options...
dachshund Posted September 4, 2009 Author Share Posted September 4, 2009 that page has a header which is included and all the connect information and session_start() is in that Quote Link to comment https://forums.phpfreaks.com/topic/173151-solved-_sessionuid-wont-stay-open/#findComment-912658 Share on other sites More sharing options...
bundyxc Posted September 4, 2009 Share Posted September 4, 2009 Did you maybe accidentally capitalize 'uid' somewhere? Variable names are case sensitive. Quote Link to comment https://forums.phpfreaks.com/topic/173151-solved-_sessionuid-wont-stay-open/#findComment-912664 Share on other sites More sharing options...
dachshund Posted September 4, 2009 Author Share Posted September 4, 2009 no i can't find any that are capitalized Quote Link to comment https://forums.phpfreaks.com/topic/173151-solved-_sessionuid-wont-stay-open/#findComment-912672 Share on other sites More sharing options...
bundyxc Posted September 4, 2009 Share Posted September 4, 2009 Please post the header script so that we can take a look at that. Also, I don't see an include/require in your login form... any chance you just forgot that? Quote Link to comment https://forums.phpfreaks.com/topic/173151-solved-_sessionuid-wont-stay-open/#findComment-912676 Share on other sites More sharing options...
dachshund Posted September 4, 2009 Author Share Posted September 4, 2009 Here's the log in form in full and the header below that. <div id="site_container"> <? include "../template/header.php"; ?> <div id="content"> <div id="viewlogin"> <div id="viewtitle"> <?php if(isset($_SESSION['uid'])){ echo 'Account'; }else { echo 'Log In'; } ?> </div> <div id="viewcontent"> <?php if(isset($_SESSION['uid'])){ $sessionname = $_SESSION['uid']; $accountsql = "SELECT * FROM users WHERE `userid` = '$sessionname'"; $accountres = mysql_query($accountsql) or die(mysql_error()); $accountrow = mysql_fetch_assoc($accountres); echo 'Hello '; echo $accountrow['username']; echo ' ( '; echo $accountrow['email']; echo ' ).'; echo '<div id="profilepicture">'; echo '<img src="/images/user/'; echo $accountrow['userid']; echo '/profilepicture.jpg" width="60" height="60">'; echo '</div>'; echo 'Upload a new icon'; }else { if(!$_POST['login']){ echo '<form name="login" method="post" action="#">'; echo '<p>Username: <input type="text" name="username"></p>'; echo '<p>Password: <input type="password" name="password"></p>'; echo '<p><input type="submit" name="login" value="Submit"></p>'; echo '</form>'; }else { $username = $_POST['username']; $password = $_POST['password']; if($username && $password){ $sql = "SELECT * FROM users WHERE `username`='$username'"; $res = mysql_query($sql) or die(mysql_error()); if(mysql_num_rows($res) == 1){ $epass = md5($password); $sql2 = "SELECT * FROM users WHERE `username` = '$username' AND `password` = '$epass'"; $res2 = mysql_query($sql2) or die(mysql_error()); if(mysql_num_rows($res2) == 1){ //success $rows = mysql_fetch_assoc($res2); $_SESSION['uid'] = $rows['userid']; echo 'You are logged in as '; echo $username; }else { echo 'Wrong username and/or password'; } }else { echo 'Username does not exist.'; } }else { echo 'Please fill in all fields.'; } } } ?> </div> </div> <div id="rightcolumn"> <?php include "../mostpopular.php"; ?> <div id="spacer"></div> <?php include "../news.php"; ?> </div> <div class="clear"></div> </div> <? include "../template/footer.php"; ?> </div> <?php $connect= mysql_connect ("-", "-", "-") or die(mysql_error()); $db = mysql_select_db ("-", $connect); session_start(); ?> <link href="/template/stylesheet.css" rel="stylesheet" type="text/css" /> <!-- TOOLBAR --> <div id="toolbar"> <div id="follow"> <ul> <li> <img src="h/images/follow/twitter_icon.png" /> </li> <li> <img src="/images/follow/facebook_icon.png" /> </li> <li> <img src="/images/follow/myspace_icon.png" /> </li> <li> <img src="/images/follow/rss_icon.png" /> </li> </ul> </div> <div id="usertools"> <div id="login"> <ul> <li class="login_text"> <?php if(isset($_SESSION['uid'])){ echo '<li class="login_text"><a href="'; include $_SERVER['DOCUMENT_ROOT']; echo '/test2/profile/">Account</a></li>'; echo '<li class="login_text"><a href="'; include $_SERVER['DOCUMENT_ROOT']; echo '/test2/profile/logout.php">Logout</a></li>'; }else { echo '<li class="login_text"><a href="'; include $_SERVER['DOCUMENT_ROOT']; echo '/test2/profile/">Log In</a></li>'; echo '<li class="login_text"><a href="'; include $_SERVER['DOCUMENT_ROOT']; echo '/test2/profile/register.php">Register</a></li>'; } ?> </ul> </div> <form id="search_form"> <input type="text" id="search_form" class="login_text" value="Search"> </form> <div class="clear"></div> </div> <div class="clear"></div> </div> <!-- HEADER --> <div id="header"> HUH. Magazine </div> <!-- NAVIGATION --> <div id="navigation"> <ul> <li> <a href="<?php include $_SERVER['DOCUMENT_ROOT']; ?>/test2/index.php"> Home </a> </li> <li> <a href="<?php include $_SERVER['DOCUMENT_ROOT']; ?>/test2/feature/"> Features </a> </li> <li> <a href="<?php include $_SERVER['DOCUMENT_ROOT']; ?>/test2/interview/"> Interviews </a> </li> <li> <a href="<?php include $_SERVER['DOCUMENT_ROOT']; ?>/test2/review/"> Reviews </a> </li> <li> <a href="<?php include $_SERVER['DOCUMENT_ROOT']; ?>/test2/blog/"> Blog </a> </li> <li> <a href="<?php include $_SERVER['DOCUMENT_ROOT']; ?>/test2/shop/"> Shop </a> </li> <li> <a href="<?php include $_SERVER['DOCUMENT_ROOT']; ?>/test2/tv/"> TV </a> </li> </ul> </div> Quote Link to comment https://forums.phpfreaks.com/topic/173151-solved-_sessionuid-wont-stay-open/#findComment-912678 Share on other sites More sharing options...
dachshund Posted September 4, 2009 Author Share Posted September 4, 2009 anyone spot anything? or is there some code i can put in the header which might help point out what's wrong? Quote Link to comment https://forums.phpfreaks.com/topic/173151-solved-_sessionuid-wont-stay-open/#findComment-912695 Share on other sites More sharing options...
bundyxc Posted September 4, 2009 Share Posted September 4, 2009 Is the rest of the header showing up? Quote Link to comment https://forums.phpfreaks.com/topic/173151-solved-_sessionuid-wont-stay-open/#findComment-912699 Share on other sites More sharing options...
dachshund Posted September 4, 2009 Author Share Posted September 4, 2009 yes header is all fine. but it's showing log in and register instead of account and log out (which it would do if it was logged in) Quote Link to comment https://forums.phpfreaks.com/topic/173151-solved-_sessionuid-wont-stay-open/#findComment-912704 Share on other sites More sharing options...
bundyxc Posted September 4, 2009 Share Posted September 4, 2009 After the line with session_start, add this: die(print_r($_SESSION)); Quote Link to comment https://forums.phpfreaks.com/topic/173151-solved-_sessionuid-wont-stay-open/#findComment-912720 Share on other sites More sharing options...
dachshund Posted September 4, 2009 Author Share Posted September 4, 2009 returns Array ( ) 1 Quote Link to comment https://forums.phpfreaks.com/topic/173151-solved-_sessionuid-wont-stay-open/#findComment-912747 Share on other sites More sharing options...
corbin Posted September 4, 2009 Share Posted September 4, 2009 Sounds to me like headers have already been sent. Do you get an error about already sent headers anywhere? If you do this: if(!headers_sent()) { session_start(); } else { echo "Already sent."; } What happens? Quote Link to comment https://forums.phpfreaks.com/topic/173151-solved-_sessionuid-wont-stay-open/#findComment-912757 Share on other sites More sharing options...
dachshund Posted September 5, 2009 Author Share Posted September 5, 2009 it echo's already sent. how can i fix that? Quote Link to comment https://forums.phpfreaks.com/topic/173151-solved-_sessionuid-wont-stay-open/#findComment-913066 Share on other sites More sharing options...
bundyxc Posted September 5, 2009 Share Posted September 5, 2009 I'm just learning about HTTP headers... so this is just a stab in the dark: in your header file, delete everything before the first occurrence of "<?php". Blank lines, spaces, etc. The file should start out with "<?php" at the very top of the file. Quote Link to comment https://forums.phpfreaks.com/topic/173151-solved-_sessionuid-wont-stay-open/#findComment-913071 Share on other sites More sharing options...
dachshund Posted September 5, 2009 Author Share Posted September 5, 2009 yeah i don't know about HTTP headers either. I don't think I have one? I just include "../template/header.php". there are no spaces at the top of the header file. it starts with <?php Quote Link to comment https://forums.phpfreaks.com/topic/173151-solved-_sessionuid-wont-stay-open/#findComment-913076 Share on other sites More sharing options...
PFMaBiSmAd Posted September 5, 2009 Share Posted September 5, 2009 You are including the "../template/header.php" file AFTER you have output a line of HTML to the browser. The session_start() must be on the page before any HTML is output. Quote Link to comment https://forums.phpfreaks.com/topic/173151-solved-_sessionuid-wont-stay-open/#findComment-913080 Share on other sites More sharing options...
bundyxc Posted September 5, 2009 Share Posted September 5, 2009 In your login form, make the change below... Take this: <div id="site_container"> <? include "../template/header.php"; ?> And change it to this: <? include "../template/header.php"; ?> <div id="site_container"> Quote Link to comment https://forums.phpfreaks.com/topic/173151-solved-_sessionuid-wont-stay-open/#findComment-913082 Share on other sites More sharing options...
dachshund Posted September 5, 2009 Author Share Posted September 5, 2009 ah finally. thanks, that worked. so i'll put <div id="site_container"> in my header file instead, and </div> in my footer.php thanks! Quote Link to comment https://forums.phpfreaks.com/topic/173151-solved-_sessionuid-wont-stay-open/#findComment-913084 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.