Jump to content

[SOLVED] some session stuff (almost have it working)


Recommended Posts

<?
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.

Link to comment
Share on other sites

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. :P

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.