会话变量在页面刷新时重置

时间:2021-03-23 21:15:01

I`m trying to create a mock login for a website but whenever I try to save the username in a session, when the page reloads, it resets the session to 1. I put on session.auto_start on thinking it was a session_start issue but it's still happening

我试图为一个网站创建一个模拟登录,但是每当我尝试在会话中保存用户名时,当页面重新加载时,它会将会话重置为1。我穿上会话。auto_start认为这是一个session_start问题,但它仍然在发生

my header.php

我的header。php

    <?php
        if($_SESSION['user'] != 1){
            $user = $_SESSION['user'];
    ?>
        <input type='submit' id='accEdit' value='Edit Account'/>
        <label class='clickLog'>Welcome: <?=$user?></label>
    <?php
        }else{
    ?>
    <input type='hidden' id='accEdit' value='Edit Account'/>
    <p id="clickLog">Login?</p>
    <?php
        }

    ?>

the function

这个函数

 case "login":

        $user = $_POST['user'];
        $pass = $_POST['pass']; 
        if($user = "Cody" && $pass = "1234"){
            $_SESSION['user'] = $user;
            echo "yes";
        }else{
            echo "no";
        }
        exit;

the ajax call

ajax调用

function login(user,pass){
    var action = "login";
    $.ajax({
        url:'appedit.php',
        type: "POST",
        data: {action:action,user:user,pass:pass},
        success: function(response){
            if(response == "yes"){
                alert(response);
                $("#clickLog").html("<label class='clickLog'>Welcome: " + $user + "</label>");
                $("#clickLog").show();
                $("#accEdit").prop("type", "submit");
            }else{
                alert(response + "fail");
                $("#clickLog").show();
            }
        },              
        error: function (request, status, error) {
            alert(error);
        }
    });
}

I know that it's not the best looking code but I'm just trying to patch something together.

我知道这不是最好的代码,但我只是想拼凑一些东西。

1 个解决方案

#1


3  

You're assigning here

你在这里分配

if($user = "Cody" && $pass = "1234"){
         ^                 ^

where you should be comparing

你应该在哪里比较

if($user == "Cody" && $pass == "1234"){
         ^^                 ^^

using 2x equal signs.

使用2 x =迹象。

  • 1 equal sign => assign
  • 1等号=>赋值
  • 2 equal signs => compare
  • 2个等号=>比较
  • 3 equal signs => if identical
  • 3个等号=>,如果相同

Consult the manual http://php.net/manual/en/language.operators.comparison.php

查阅手册http://php.net/manual/en/language.operators.comparison.php

Plus, do make sure that the session was indeed started session_start(); inside all pages using sessions.

另外,请确保会话确实已启动session_start();在所有页面中使用会话。

  • Use isset().
  • 使用收取()。
  • Use !empty().
  • 使用!空()。

Add error reporting to the top of your file(s) which will help find errors.

将错误报告添加到文件的顶部,这将有助于查找错误。

<?php 
error_reporting(E_ALL);
ini_set('display_errors', 1);

// rest of your code

Sidenote: Error reporting should only be done in staging, and never production.

Sidenote: Error reporting(错误报告)仅在staging中完成,而不是在产品中完成。

  • Check your console.
  • 检查你的控制台。

Additionally If you're planning to move forward with creating a login system don't limit passwords and use the proper methods to hash and verify passwords with PHP.

此外,如果您打算继续创建登录系统,请不要限制密码,并使用适当的方法使用PHP对密码进行哈希和验证。

If you plan to continue using the AJAX function provided by jQuery you should be aware that certain return functions have been deprecated and will be removed soon:

如果您打算继续使用jQuery提供的AJAX功能,您应该知道某些返回函数已经被弃用,并且将很快被删除:

Deprecation Notice: The jqXHR.success(), jqXHR.error(), and jqXHR.complete() callbacks are deprecated as of jQuery 1.8. To prepare your code for their eventual removal, use jqXHR.done(), jqXHR.fail(), and jqXHR.always() instead.

弃用通知:jqXHR.success()、jqXHR.error()和jqXHR.complete()回调在jQuery 1.8中被弃用。要准备最终删除代码,可以使用jqXHR.done()、jqXHR.fail()和jqXHR.always()。

#1


3  

You're assigning here

你在这里分配

if($user = "Cody" && $pass = "1234"){
         ^                 ^

where you should be comparing

你应该在哪里比较

if($user == "Cody" && $pass == "1234"){
         ^^                 ^^

using 2x equal signs.

使用2 x =迹象。

  • 1 equal sign => assign
  • 1等号=>赋值
  • 2 equal signs => compare
  • 2个等号=>比较
  • 3 equal signs => if identical
  • 3个等号=>,如果相同

Consult the manual http://php.net/manual/en/language.operators.comparison.php

查阅手册http://php.net/manual/en/language.operators.comparison.php

Plus, do make sure that the session was indeed started session_start(); inside all pages using sessions.

另外,请确保会话确实已启动session_start();在所有页面中使用会话。

  • Use isset().
  • 使用收取()。
  • Use !empty().
  • 使用!空()。

Add error reporting to the top of your file(s) which will help find errors.

将错误报告添加到文件的顶部,这将有助于查找错误。

<?php 
error_reporting(E_ALL);
ini_set('display_errors', 1);

// rest of your code

Sidenote: Error reporting should only be done in staging, and never production.

Sidenote: Error reporting(错误报告)仅在staging中完成,而不是在产品中完成。

  • Check your console.
  • 检查你的控制台。

Additionally If you're planning to move forward with creating a login system don't limit passwords and use the proper methods to hash and verify passwords with PHP.

此外,如果您打算继续创建登录系统,请不要限制密码,并使用适当的方法使用PHP对密码进行哈希和验证。

If you plan to continue using the AJAX function provided by jQuery you should be aware that certain return functions have been deprecated and will be removed soon:

如果您打算继续使用jQuery提供的AJAX功能,您应该知道某些返回函数已经被弃用,并且将很快被删除:

Deprecation Notice: The jqXHR.success(), jqXHR.error(), and jqXHR.complete() callbacks are deprecated as of jQuery 1.8. To prepare your code for their eventual removal, use jqXHR.done(), jqXHR.fail(), and jqXHR.always() instead.

弃用通知:jqXHR.success()、jqXHR.error()和jqXHR.complete()回调在jQuery 1.8中被弃用。要准备最终删除代码,可以使用jqXHR.done()、jqXHR.fail()和jqXHR.always()。