【问题标题】:PHP Session won't start [closed]PHP 会话不会开始[关闭]
【发布时间】:2018-10-15 17:53:40
【问题描述】:

我在使用 PHP 启动会话时遇到问题。通过环顾四周,我写了一些应该可以工作但没有用的代码。你能帮帮我吗,因为我不知道这里出了什么问题。这是我的 logging.php 页面

    <?php

$host = "localhost";
$user = "usern";
$password = "gtest123";
$db = "test";
$errore = "Login info are wrong!`enter code here`";

mysql_connect($host,$user,$password);
mysql_select_db($db);

if(isset($_POST['username'])){

    $username = $_POST['username'];
    $password = $_POST['password'];

    $sql = "select * from utenti where username = '".$username."' AND Password = '".$password."' limit 1";

    $result = mysql_query($sql);

        if(mysql_num_rows($result)==1){
            $_SESSION['username'] = $username;
            header("location:index.php");
        }

        else{
            echo "" .$errore;
        }`enter code here`
}

    ?>

我的数据库与 phpmyamin 上的用户一起使用,并且登录它正在工作。问题是当我加载 index.php 页面时。

<?php
    session_start();
    echo "Welcome" .$_SESSION[''];
?>
<html>
all the html code

我开始这个会话是因为我希望能够看到哪个用户在网站上执行某些功能。但是我收到此错误消息: 注意:未定义的索引: 我知道错误是什么意思,但我不知道如何解决它,有什么帮助吗?

【问题讨论】:

  • echo "Welcome" .$_SESSION['username'];
  • 您需要在每个页面上致电session_start。现在,在你的 login.php 中,你正试图将项目塞入一个你甚至还没有开始的会话中。
  • 您应该定义要显示的会话。试试$_SESSION['username']
  • 每次在新代码中使用the mysql_ 数据库扩展this happens 时,它都已被弃用,并且已经存在多年,并且在 PHP7.0+ 中永远消失了。如果您只是学习 PHP,请花精力学习 PDOmysqli 数据库扩展和预处理语句。 Start here
  • 小点 phpMyAdmin is a tool written in PHP to make fiddling with you **MYSQL** database easier then having to use the command line. So MYSQL is a database and phpMyAdmin`只是一个工具

标签: php html session undefined-index


【解决方案1】:

所以,首先,我看到您正在使用 mysql_connect,这是一个已弃用的函数,因为它根本不安全,它已被 mysqli_connect http://php.net/manual/en/book.mysqli.php 替换为文档。为了获得更好的安全性并防止 sql 注入,您应该使用 PDO 或准备好的语句。不过,在这个例子中,我坚持使用mysqli,因为它的学习曲线较少。

其次,$_SESSION 仅在您首先使用 session_start() 初始化会话时才有效。这必须在您希望从中读取或写入会话数据的每个页面上完成。

<?php

    //Since this page writes to a session, initialise it here
    session_start();

    //The values to connect to the database with
    $host = "localhost";
    $user = "usern";
    $password = "gtest123";
    $db = "test";

    //Create a new mysqli connection to the database
    $conn = mysqli_connect($host, $user, $password, $db);


    //This is the error message that's displayed on unsuccessful login
    $error = "Login info are wrong!`enter code here`";

    //This is the error message if the username is not specified
    $errorNoUsername = "You have not specified a username";

    /**
    *  Now that we're using mysqli_connect(), we don't need this code.
    *  mysql_connect($host,$user,$password);
    *  mysql_select_db($db);
    **/

    //See if the user has submitted the form with the username parameter
    if(isset($_POST['username'])){
        //If they have, shortname the variable for username and password
        $userUsername = $_POST['username'];
        $userPassword = $_POST['password'];

        //Build your select query. In production, you should use PDO or Prepared Statements to protect against injection
        //I've removed your LIMIT 1 from the query, because I see you're checking for a distinct match later on with mysqli_num_rows==1
        $sql = "SELECT * FROM utenti WHERE username='".$userUsername."' AND Password = '".$userPassword."'";

        //run the query on the connection created earlier
        $result = mysqli_query($conn, $sql);

        //Check if there's a distinct match
        if(mysqli_num_rows($result)==1){
            //There is, good, initialise session with the user data
            $_SESSION['username'] = $userUsername;

            //Reload to your index.php page
            header("location:index.php");
        } else {
            //Display the error message
            echo $error;
        }
    } else {
        echo $errorNoUsername;
    }
?>

既然我们已经这样做了,假设登录成功,我们将用户重定向回index.php,因为我们正在读取会话数据,我们需要再次初始化会话,使用session_start();,这您已经完成了,但是您的密钥 $_SESSION[''] 不存在,因此出现错误。在这里,我已经更正了。

<?php
    session_start();
    echo "Welcome, " . $_SESSION['username']; //Added keyname
?>
<html>
    all the html code
</html>

【讨论】:

  • 这可行,但唯一的问题是当它加载 index.php 时,它不会显示用户名。它只显示“欢迎光临”
  • 我的错,我犯了一个错误。我将$username 更改为$userUsername。我已经更新了我的答案。 (其中设置了$_SESSION
  • 其实这是我的错,我没有意识到你改变了 var 名称。谢谢!
【解决方案2】:

在您要处理会话的每个页面中使用session_start(),并且在loging.php 页面中设置$_SESSION['username'],因此您需要更改

echo "Welcome" .$_SESSION[''];

echo "Welcome" .$_SESSION['username'];

这样你就可以在index.php中得到你在loging.php页面中设置的username的会话

【讨论】:

  • 我在 login.php 中启动了会话并按照您的建议更改了名称,但仍然出现错误:注意:未定义索引:C:\xampp\htdocs\si\index.php 中的用户名在第 3 行
  • 好的,删除index.php中session_start();前面的多余空格
  • 删除两个页面中php标签和session_start()之前的多余空格
  • 修复了,但我不知道为什么。这是有特定原因的还是 session_start() 不喜欢前面有多余的空间?
  • 是的。永远不要在 php 标签和 session_start() 之前允许任何额外的空间
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-02-12
  • 1970-01-01
  • 2014-02-13
  • 1970-01-01
  • 2023-03-16
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多