会话注销而不关闭会话

时间:2017-02-18 12:58:15

标签: php session

我们有一个会话注销脚本,如:

<?php
    //24 2 2015
    session_start();
    session_destroy();
    header("location:login.php")
?>

现在此脚本注销并将其重定向到登录页面,其中需要用户名和密码才能再次登录。

如果我想要一个临时注销,在注销后会将我们引导到一个只需要密码的登录页面,因为会话没有被销毁而且用户名被传递到该页面... < / p>

因此,当您输入密码时,它将检查数据库表中的输入,其中username = session username。

希望我很清楚。

更新::

templogout.php

<?php
    //24 2 2015
    session_start();
    $_SESSION['temp_logout'] = true;
    header("location:templogin.php")
?>

templogin.php

<?php
    //24 2 2015
    session_start();
?>
<form id="msform" action="templogincheck.php" method="post">
  <fieldset>
      <input type="password" name="password" placeholder="Enter password here" required />
    <button type="submit" name="submit" class="submit action-button"> LogIn </button>
</form>

templogincheck.php

<?php
    //15 2 2015
    session_start();

    $Cser =mysqli_connect("localhost","text","text","text") or die("Server connection failed : ".mysqli_error($Cser));

    $password = md5($_REQUEST["password"]);
    $mobile = $_SESSION['mobile'];

    $s = "select * from users where password = '".$password."' and mobile = '".$mobile."'";
    $result = mysqli_query($Cser,$s);
    $count = mysqli_num_rows($result);
    if($count>0)
    {
        $_SESSION["mobile"] = $mobile;
        $_SESSION["login"]="1";
        header("location:/index.php");
    }
    else
    {
     header("location:/templogin.php");   
  }
?>

的index.php

<?php 
    //15 2 2015
    session_start();
        unset($_SESSION["temp_logout"]);
    if(!isset($_SESSION["login"]))
        header("location:login.php");
?>

我希望我做得对,但我必须假设我有错误,因为它不起作用..

我是否将会话移动到了登录检查页面?

用户首次登录页面:

<form id="msform" action="ulogincheck.php" method="post">
  <fieldset>
    <h2 class="fs-title">LogIn</h2>
    <h3 class="fs-subtitle">Please Enter your details accordingly<br/><br/> <small>(case sensitive)</small></h3>
      <input type="text" name="email" placeholder="Email" required />
      <input type="text" name="mobile" placeholder="Mobile" required />
      <input type="password" name="password" placeholder="Password" required />
    <button type="submit" name="submit" class="submit action-button"> LogIn </button>
</form>

首次登录检查页面

session_start();
$email = $_REQUEST["email"];
    $mobile = $_REQUEST["mobile"];
    $password = md5($_REQUEST["password"]);


    $s = "select * from users where email='".$email."' and password = '".$password."' and mobile = '".$mobile."'";

    $result = mysqli_query($Cser,$s);

    $count = mysqli_num_rows($result);

    if($count>0)
    {
        $_SESSION["email"] = $email; 
        $_SESSION["mobile"] = $mobile;
        $_SESSION["login"]="1";
        header("location:/index2.php");
    }
    else
    {
           header("location:/usersignin.php");   

2 个答案:

答案 0 :(得分:1)

您可以向$_SESSION变量添加$_SESSION["temp_logout"]字段,当您将用户重定向到登录页面时,可以检查它<?php //24 2 2015 session_start(); $_SESSION['temp_logout'] = true; header("location:login.php") ?> ,如果是,则添加输入字段中的用户名。

注销脚本:

session_start()
...
//where the "username" input is
<input name="username" <?php if(isset($_SESSION["temp_logout"]){
    echo 'value="'.$_SESSION["username"] .'" ';
} ?> />
...

登录页面:

<?php
    session_start();
    unset($_SESSION["temp_logout"]);
?>

成功登录后:

{{1}}

此外,在网站的任何地方,不要忘记检查用户是否暂时退出;然后立即将他重定向到登录页面

答案 1 :(得分:0)

它真的取决于您的平台: 你只能取消password之类的东西,而不是破坏会话,

unset($_SESSION['password']);

或在会话中设置另一个键:

$_SESSION['loggedIn'] = false;

并重定向到登录页面。

你也可以在cookie中输入用户名并销毁会话。

setcookie

如果您想在cookie中存储用户名,最好是出于安全原因对其进行加密。