$_SESSION['user'] 不工作 [英] $_SESSION['user'] not working

查看:51
本文介绍了$_SESSION['user'] 不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以这是记录的代码和我设置的地方

SO this is the code for logging and and where I set things

<?php 
    session_start();
    $_SESSION['user'] = "kjkj";
    $_SESSION['pass'] = "";  
    $error = $user = $pass = "";

    if (isset($_POST['user'])) {
        $user = sanitizeString($_POST['user']);
        $pass = sanitizeString($_POST['pass']);

     if ($user == "" || $pass == "") {
            $error = "Not all fields were entered<br />";
     } else {
            $query = "SELECT store,c_pass FROM store
                    WHERE store='$user' AND c_pass='$pass'";

            if (mysql_num_rows(queryMysql($query)) == 0) {
                $error = "Username/Password invalid<br />";
            } else {
            $_SESSION['user'] = $user;
                $_SESSION['pass'] = $pass;
            $str = $_SESSION['user'] . ", You are now logged in. Please 
                       <a href='scheduler.php'>click here</a>.";

                die($str);
            }
        }
    } ?>

它会在查询和所有这些之后打印正确的商店名称.但是当我尝试在这样的另一个 php 文件中使用它时

It'll print the correct store name after the query and all that. But when I try to use it in another php file like this

if (!isset($_SESSION['user']) ) {
    die("<p><h1>Please Login</h1></p>");
} else {
    echo "<p><form id ='addemp' method=\"post\" action=\"addUser.php\">
          Name<input type=\"text\" name=\"emp\" />
      \"". $_SESSION['user'] . "\">
      <input type=\"submit\" value=\"AddUser\" />
    </form></p>";
}

这是一个空字符串.不为空只是空字符串.我尝试了所有可以在互联网上找到的解决方案,但都没有奏效.我不知道为什么这不起作用.任何帮助将不胜感激,谢谢.

It is an empty string. Not null just empty string. I tried all the solutions I can find on the internet, none of them worked. I'm out ideas as to why this isn't working. Any help will be greatly appreciated, thank you.

推荐答案

可能有很多方面.首先,会话是否在其他时间有效?我认为您没有提供足够的信息让我们为您提供帮助.这可能是 apache/php 设置的问题,而不仅仅是您的代码.之前我在 Windows 上使用 WAMP 进行开发时发生过这种情况,临时文件夹没有正确的权限.正如我所说,可能有很多问题会导致您的会话行为不端.

It could be a number of things. First of all do sessions work any other time? I don't think you have provided enough information for us to help you. It could be a problem with set-up of apache/php not just your code. Has happened to me before when I was developing on Windows with WAMP and temp folder didn't have correct permissions. As I said there could be many issues that cause your session to misbehave.

  • 当你做一个计数器并刷新时页面是否保留了一个数字?
  • 开始每次使用您需要的会话调用 session_start() 方法.重要:之前不能有任何回声或印刷品等session_start().
  • var_dump($user) 放在 $_SESSION['user'] = $user; 之前,并在保存之前检查 $user 的内容.可能是您的消毒功能工作不正常.在第一个脚本的末尾执行此操作以查看 $_SESSION 的内容,以确保正确保存变量.
  • When you do a counter and refresh page does it keep a number?
  • At the beginning of every time that uses sessions you need to have session_start() method called. Important: There can't be any echo's or prints etc before session_start().
  • Put var_dump($user) before $_SESSION['user'] = $user; and check the content of $user before it gets saved. It could be that your sanitizing function is not working properly. Do it also at the end of the first script to see the content of $_SESSION to make sure variables are saved properly.

这篇关于$_SESSION['user'] 不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆