注销代码根本不执行。 PHP

时间:2010-02-28 02:22:13

标签: php

这是代码

我不知道它有什么不对。

<?php
       //Logout code
       //Starting Session
       session_start();
       //Include
       include ("includes/mass.php");
       //Check if the user is logged in
       $username = $_SESSION['username'];
       $logged_in_query = "SELECT * FROM    user WHERE loggedin='1' AND    username='$username'";
       $check_if_logged_in =    mysql_query($logged_in_query);
       if (isset($username))

    {       while ($row =    mysql_fetch_array($check_if_logged_in))

            {
                $logged_in = $row['loggedin'];

                if ($logged_in == 1)

                    {
                    //User becomes logged out on database records

                    $sql_logout = "UPDATE user SET loggedin='0' WHERE loggedin='1' AND    username='$username'";

                    $logout_query = mysql_query($logout_query);

                    //Logout page

                    session_destroy();

                    echo "You have been logged out.","<br>"."<a    href='index.php'>Click Here To Go    Back</a>";
                    }
                            }
                }                else

    {
                                echo"You are not logged in"."<br><a href='register.php'>Click    To Sign Up</a>";
                            }           
         ?>

3 个答案:

答案 0 :(得分:3)

你有一个mysql链接对象(来自mysql_connect()/ mysql_select_db()吗?)从下面的评论来看,听起来不是这样。

这个SQL错了:

$sql_logout = "UPDATE user WHERE loggedin='1' AND username='$username'";

应该是:

$sql_logout = "UPDATE user SET loggedin=0 WHERE loggedin='1' AND username='$username'";

你可能也意味着使用mysql_fetch_assoc()而不是mysql_fetch_array()。

这一行:

$logout_query = mysql_query($logout_query);

应该是

$logout_query = mysql_query($sql_logout);

输入正确的mysql连接和数据库信息,然后尝试运行它。请发布输出。

<?php
//Logout code
//Starting Session
session_start();

echo "hello<br />";

//Include
include ("includes/mass.php");

echo "no problem in mass.php!<br />";

// FILL ME IN
$my_link = mysql_connect($server, $username, $password, TRUE);
mysql_select_db('your_db', $link);

//Check if the user is logged in
$username = $_SESSION['username'];
$logged_in_query = "SELECT loggedin FROM user WHERE loggedin='1' AND username='$username'";
echo $logged_in_query . "<br />";

$check_if_logged_in = mysql_query($logged_in_query, $my_link);
var_dump(mysql_num_rows($check_if_logged_in));

if (isset($username))
{
    while ($row = mysql_fetch_assoc($check_if_logged_in))
    {
        var_dump($row);

        $logged_in = $row['loggedin'];

        if ($logged_in == 1)
        {
            //User become logged out on database records
            $sql_logout = "UPDATE user SET loggedin=0 WHERE loggedin='1' AND username='$username'";
            $logout_query = mysql_query($sql_logout, $my_link);

            //Logout page
            session_destroy();
            echo "You have been logged out.","<br>"."<a href='index.php'>Click Here To Go Back</a>";
        }
        else
        {
            echo"You are not logged in"."<br><a href='register.php'>Click To Sign Up</a>";
        }
    }
}
?>

答案 1 :(得分:1)

你写的是非常糟糕的代码。我建议你这样做 1.一旦用户名和密码与db中的条目匹配,就在登录页面中创建一个会话 2.当他们说退出时,销毁那个会话。

使用db检查用户的实现是不可扩展的。每次它被执行,而不是正确的想法。

答案 2 :(得分:0)

我会用这样的东西:

    <?php
    //Logout code
    //Starting Session
    session_start();
    //Include
    include ("includes/mass.php");
    //Check if the user is logged in
    $username = $_SESSION['username'];

    if (isset($username))
    {
     $logged_in_query = "SELECT * FROM user WHERE loggedin='1' AND username='".$username."' LIMIT 1";
     $check_if_logged_in = mysql_query($logged_in_query);
     $logged_in = mysql_fetch_field($check_if_logged_in);

      if ($logged_in == 1)
      {
       //User becomes logged out on database records

       $sql_logout = "UPDATE user SET loggedin='0' WHERE loggedin='1' AND username='".$username."' LIMIT 1";

       $logout_query = mysql_query($logout_query);
       if ($logout_query)
       {
        //Logout page

        session_destroy();
        echo "You have been logged out.","<br>"."<a href='index.php'>Click Here To Go Back</a>";
       }
       else
       {
        //Couldn't update the user table to set your login status.
        echo "MYSQL Error, please contact admin LO-2";
        exit();
       }
      }
      else
      {
       echo "You are not logged in"."<br><a href='register.php'>Click To Sign Up</a>";
      }
     }
     else
     {
      echo "You are not logged in"."<br><a href='register.php'>Click To Sign Up</a>";
     }
?>

未经测试

最高