0

我编写了一个使用会话和 cookie 的 php 登录脚本。我知道关闭浏览器后会话会过期,但 cookie 会一直存在,直到达到过期时间。在我的脚本中,我创建了一个 COOKIE 来存储登录用户的“用户名”。登录后,如果我关闭浏览器并重新打开它..由于cookie,我必须仍然保持登录状态。当我点击注销时,我必须注销。但是我的脚本无法正常工作,因为在单击“注销”按钮链接后,我的脚本没有将我注销。

这是我写的php代码:

    <?php
session_start();
require_once('login/connectvars.php');
    if(!isset($_SESSION['username']) && isset($_COOKIE['username']))
{
    $_SESSION['username'] = $_COOKIE['username'];
}

if(isset($_POST['submit']))
        {
          if(!empty($_POST['username']) && !empty($_POST['password']))
          {
            $dbc = mysqli_connect(DB_HOST,DB_USER,DB_PASSWORD,DB_NAME);
            $username = mysqli_real_escape_string($dbc,trim($_POST['username']));
            $password = mysqli_real_escape_string($dbc,trim($_POST['password']));

            $query = "SELECT user_id,username FROM users_dns WHERE username = '$username' AND password = SHA('$password')";
            $data = mysqli_query($dbc,$query) or die(mysqli_error($dbc));
            if(mysqli_num_rows($data) == 1)
            {
                $row = mysqli_fetch_array($data);
                $_SESSION['user_id'] = $row['user_id'];
                $_SESSION['username'] = $row['username'];
                setcookie('username',$row['username'],time() + (60 * 60));
                echo 'success';
                mysqli_close($dbc);
            }    
            else
            {
                echo 'enter correct username and password';
            }
         }
        else
        {
            echo 'no empty fields please..';
        }
      }

?>

这是我在 html 代码中使用的内容:

<?php
       if(isset($_SESSION['username']))
       {
         echo 'logged in as'.$_SESSION['username'];
         echo '<a href="login/logout.php">log out ('.$_SESSION['username'].')</a><br />';
         echo $_SESSION['username'];
       }
       else
       {
       ?>
       <div id="login_form">

        <!--<span class="error"><?php echo $error_msg; ?></span>-->
         <form id="login"  method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
            <table>
                  <tr>
                     <th>Username:</th>
                     <th>Password:</th>
                  </tr>
                  <tr>
                     <td><input type="text" name="username" /></td>
                     <td><input type="password" name="password" /></td>
                   </tr>
                   <tr>
                      <td><input type="submit" name="submit" value="Log In" class="submit" /></td>
                      <td><a href="login/signup.php"><input type="button" class="submit" value="Sign Up" /></a></td>
                   </tr>
            </table>
         </form>
       </div>
       <?php
       }
       ?>

这是我的注销脚本

    <?php
session_start();

if(isset($_SESSION['username']))
{
    $_SESSION = array();

if(isset($_COOKIE['session_name()']))
{
    setcookie('username','',time() - 3600);
}
session_destroy();
}
setcookie('user_id','',time() - 3600);
setcookie('username','',time() - 3600);

echo 'redirecting you...please wait..';
header("Refresh: 3;url=http://localhost/");
?>
4

1 回答 1

1
if(isset($_COOKIE['session_name()']))

这应该是

if(isset($_COOKIE['username']))

因为您已经创建了usernamecookie 并且正在检查session_name()cookie。

于 2013-01-24T11:50:45.260 回答