PHP在线用户---会话功能

时间:2013-09-14 03:14:49

标签: php sql login

当一个人登录时,表中的在线列设置为1,当他退出时,它被设置为0。我实现了Login脚本,但是在使用SQL查询结束会话时遇到了问题..请帮忙! 显示无错误,但即使在注销后,在线值仍为1

**LOGOUT SCRIPT**

<?php
        $offline = $_SESSION["username"] ;
        ?> 
<?php
//If the user is logged, we log him out
if(isset($offline))
{
    //We log him out by deleting the username and userid sessions
    unset($_SESSION['username'], $_SESSION['userid']);
$con=mysqli_connect("localhost","root","","chat");

mysqli_query($con,"UPDATE users SET Online=0
WHERE username='.$offline.'");

mysqli_close($con);

?>

登录脚本

<?php
    $ousername = '';
    //We check if the form has been sent
    if(isset($_POST['username'], $_POST['password']))
    {
        //We remove slashes depending on the configuration
        if(get_magic_quotes_gpc())
        {
            $ousername = stripslashes($_POST['username']);
            $username = mysql_real_escape_string(stripslashes($_POST['username']));
            $password = stripslashes($_POST['password']);
        }
        else
        {
            $username = mysql_real_escape_string($_POST['username']);
            $password = $_POST['password'];
        }
        //We get the password of the user
        $req = mysql_query('select password,id from users where username="'.$username.'"');
        $dn = mysql_fetch_array($req);
        //We compare the submited password and the real one, and we check if the user exists
        if($dn['password']==$password and mysql_num_rows($req)>0)
        {
            //If the password is good, we dont show the form
            $form = false;
            //We save the user name in the session username and the user Id in the session userid
            $_SESSION['username'] = $_POST['username'];
            $_SESSION['userid'] = $dn['id'];

$con=mysqli_connect("localhost","root","","chat");


        $sql = mysql_query('UPDATE users SET Online=1 where username="'.$username.'"');

?>
<div class="message">You have successfuly been logged. You can access to your member area.<br />
<a href="<?php echo $url_home; ?>">Home</a></div>
<?php
        }
        else
        {
            //Otherwise, we say the password is incorrect.
            $form = true;
            $message = 'The username or password is incorrect.';
        }
    }
    else
    {
        $form = true;
    }
    if($form)
    {
        //We display a message if necessary
    if(isset($message))
    {
        echo '<div class="message">'.$message.'</div>';
    }
    //We display the form
?>
<div class="content">
    <form action="connexion.php" method="post">
        Please type your IDs to log in:<br />
        <div class="center">
            <label for="username">Username</label><input type="text" name="username" id="username" value="<?php echo htmlentities($ousername, ENT_QUOTES, 'UTF-8'); ?>" /><br />
            <label for="password">Password</label><input type="password" name="password" id="password" /><br />
            <input type="submit" value="Log in" />
        </div>
    </form>
</div>

而且,当我执行SQL查询时,如何执行if Online = 1,display online.png?否则'空白'? 提前谢谢!

2 个答案:

答案 0 :(得分:2)

以下代码适用于logout.php

    <?php
//If the user is logged, we log him out
if(isset($_SESSION['username']))
{echo $_SESSION['username'];
    //We log him out by deleting the username and userid sessions

$username=$_SESSION['username'];
$sql="UPDATE users SET online=0 WHERE username='$username'";
mysql_query($sql);

unset($_SESSION['username'], $_SESSION['userid']);

?>

这个用于sign_up.php

if(isset($_POST['username'], $_POST['password'], $_POST['passverif'], $_POST['email'], $_POST['username']))

答案 1 :(得分:0)

使用session_start(); 和session_destroy();退出。