duanju9104 2017-06-27 13:27
浏览 47
已采纳

如何正确设置会话存储的用户名和cookie?

I am creating a website or social networking website. The problem is when i login without using cookie then the login script successfully echo out session stored username i.e. echo $_SESSION['username']; but when i try to login with session and cookie then it doesn't echo out session stored username! Image: [https://i.stack.imgur.com/bgniz.png][1]

Please help me! My code is:

index.php(Home)

<?php

class Redirect{

    public function redirectLogin(){
        session_start();
        if(isset($_SESSION['username']) || isset($_COOKIE['username'])){
            header('Location: logarea.php');
        }
    }
}

$red = new Redirect;
$red->redirectLogin();

?>


<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Login Form</title>
    <script src = "jquery-3.2.1.js"></script>

</head>
<body>
    <form action = "login.php" method = "post" onSubmit = "">
        Username:<input type = "text" name = "username" id = "user" /><br /><br />
        Password:<input type = "password" name = "password" id = "pass "/><br /><br />
        <input type = "checkbox" name = "rememberme" id = "remember" />Remember Me<br /><br />
        <input type = "submit" name = "login" value = "Log In" id = "login" /><br /><br />
        <p id = "responsemsg"></p>
    </form>
</body>
</html>

login.php(Login Process)

<?php

class Validate{

    protected $DB_HOST = 'localhost';
    protected $DB_USER = 'root';
    protected $DB_PASS = '';
    protected $DB_NAME = 'logindb';

    public function connect(){
        ob_start();
        session_start();
        session_regenerate_id(true);


        global $conn;

        $conn = new mysqli($this->DB_HOST, $this->DB_USER, $this->DB_PASS, $this->DB_NAME);
    }

    public function loginStatus(){
        global $conn;
        self::connect();

        if($_SERVER['REQUEST_METHOD'] == 'POST'){
            if(isset($_POST['username'])){
                $username = $_POST['username'];
            }

            if(isset($_POST['password'])){
                $password = $_POST['password'];
            }

            if(isset($_POST['rememberme'])){
                $rememberme = $_POST['rememberme'];
            }

            if(!empty($username) && !empty($password)){

                $query = "SELECT `username`, `password` FROM `login` WHERE `username` = '$username' AND `password` = '$password'";
                $query_run = $conn->query($query);

                $user = null;
                $pass = null;

                while($quo = $query_run->fetch_array()){
                    $user = $quo['username'];
                    $pass = $quo['password'];
                }

                if($username == $user && $password == $pass){
                    if(!empty($rememberme)){
                        setcookie('username', $username, time()+(24*60*60));
                    }else{
                        $_SESSION['username'] = $username;
                    }   

                    header('Location: logarea.php');
                }else{
                    echo('Incorrect Username Or Password');
                }
            }else{
                echo('Please fill username and password');
            }
        }
    }
}

$validation = new Validate;
$validation->loginStatus();

User Log Area(logarea.php)

<?php

session_start();

    if(isset($_SESSION['username']) || isset($_COOKIE['username'])){

        echo('You are now logged in, '. ucwords($_SESSION['username']));
        echo('<br />'.'<br />');
        echo('<a href = "logout.php">Log Out</a>');
    }else{
        header('Location: index.php');
    }
?>

Logout(logout.php)

<?php

session_start(); 
session_destroy();

setcookie('username' ,'', time()-(24*60*60*60));

header('Location: index.php');

Please help me.

  • 写回答

1条回答 默认 最新

  • douliu8327 2017-06-27 13:34
    关注

    You have an if else statement:

    if(!empty($rememberme)){
       setcookie('username', $username, time()+(24*60*60));
    }else{
       $_SESSION['username'] = $username;
    }  
    

    Get rid of the else. Sure, you can set a cookie to remember their username, but surely the session should be set regardless?

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 用PLC设计纸袋糊底机送料系统
  • ¥15 simulink仿真中dtc控制永磁同步电机如何控制开关频率
  • ¥15 用C语言输入方程怎么
  • ¥15 网站显示不安全连接问题
  • ¥15 github训练的模型参数无法下载
  • ¥15 51单片机显示器问题
  • ¥20 关于#qt#的问题:Qt代码的移植问题
  • ¥50 求图像处理的matlab方案
  • ¥50 winform中使用edge的Kiosk模式
  • ¥15 关于#python#的问题:功能监听网页