teenagegluesniffer Posted July 17, 2007 Share Posted July 17, 2007 <? session_start(); .... <? if ($_POST['submit']) { ..... if ($num > 0){ $query = "SELECT id FROM users WHERE username='$username' and password='$passwordthatismd5'"; $result2 = mysql_query($query) or die("Couldn't execute query."); $num2 = mysql_num_rows($result2); if ($num2 > 0){ $_SESSION['auth']="yes"; $_SESSION['loginid']="$id"; $_SESSION['loginidd'] = $id; $loggedid = "$id"; $today=date("Y-m-d h:i:s"); $sql = "INSERT INTO login (`loginid`, `logintime`) VALUES ('$loggedid', '$today')"; $result = mysql_query($query) or die ("Can't execute insert query."); echo"<div align='center'>Successfully logged in as $username!</div>"; } else { echo"<div align='center'>The password is incorrect.</div>"; } } elseif ($num == 0) { echo"<div align='center'>The username is incorrect.</div>"; } } elseif(isset( $_SESSION['loginid'])){ $sessionid = $_SESSION['loginid']; $query = "SELECT * FROM users where id='$sessionid'"; $result = mysql_query($query) or die("Couldn't execute query."); $num = mysql_num_rows($result); while ($row = mysql_fetch_row($result)); { $username = $row['username']; echo $_SESSION['loginid']; echo $_SESSION['loginidd']; echo"<div align='center'>You are already logged in as {$sessionid['$id']} $username.</div>"; session_destroy(); } } else{ ?> ...... ?> Well I tried to put the code that has the session stuff in it. Anyway, this is my login page(most of it) and i'm trying to get the sessions working! As of now, the register page I have works, and this works...up to one point. That is, you can login, but if you are logged in, and you were to go to this page, I want it to display this message: You are already logged in as --usernamehere--. But i'm new to sessions, and am having trouble getting it to display the username. If anyone can see what's wrong with my code, please let me know. Sorry if this is an annoying question, i've been trying to take care of it for days, and have had no luck - and no one has been able to help me. Thanks. Quote Link to comment Share on other sites More sharing options...
ryeman98 Posted July 17, 2007 Share Posted July 17, 2007 Couldn't you just do this: if ($sessionid['$id']) { echo "You are already logged in as {$sessionid['$id']} $username."; } Correct me if I'm wrong though... Quote Link to comment Share on other sites More sharing options...
teenagegluesniffer Posted July 17, 2007 Author Share Posted July 17, 2007 Eh, that's not working. :\ Quote Link to comment Share on other sites More sharing options...
teng84 Posted July 17, 2007 Share Posted July 17, 2007 what are this line echo"<div align='center'>You are already logged in as {$sessionid['$id']} $username.</div>"; session_destroy(); erase the session ??? it will set session array empty??? Quote Link to comment Share on other sites More sharing options...
ryeman98 Posted July 17, 2007 Share Posted July 17, 2007 what are this line echo"<div align='center'>You are already logged in as {$sessionid['$id']} $username.</div>"; session_destroy(); erase the session ??? it will set session array empty??? Can't believe I didn't post that... what's the point of logging in if you're going to destroy the sessions at the end of the page? I'd only destroy sessions on a logout page... Other than that... unset them individually if you want to unset a specific one. I'm new to sessions too. Quote Link to comment Share on other sites More sharing options...
teenagegluesniffer Posted July 17, 2007 Author Share Posted July 17, 2007 Yea, that is temporary. I haven't made a logout page yet & I have been changing the session code, so it doesn't change unless you log in again. Quote Link to comment 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.