简体   繁体   English

echo $_SESSION 坏了

[英]echo $_SESSION broken

When I run the site, this dissapears on it:当我运行该网站时,它会消失:

<?php 
echo $_SESSION["UserID"]; 
?>

I don't know why, it seems obvious it should work, it did in a video I watched?我不知道为什么,看起来很明显它应该可以工作,它在我观看的视频中做到了? It kind of works when I make it:当我制作它时,它有点工作:

<?php 
echo '$_SESSION["UserID"]'; 
?>

But then it just echo's:但它只是回声:

$_SESSION["UserID"]

And not the actual session id而不是实际的会话 ID

Here is the whole script:这是整个脚本:

<?php require 'Connections/Connections.php'; ?>
<?php
session_start();
?>

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Titled Document</title>
<link href="C:/Users/Mikkel/Desktop/HTML & CSS/bootstrap.css" rel="stylesheet" />
<link href="CSS/Layout.css" rel="stylesheet" type="text/css" />
<link href="CSS/Menu.css" rel="stylesheet" type="text/css" />
</head>
<body>
<br><?php echo $_SESSION["UserID"]; ?>
    <div id="Holder">
    <div id="Header"></div>
    <div id="NavBar">
        <nav>
            <ul>
                <li><a href="#">Login</a></li>
                <li><a href="#">Register</a></li>
                <li><a href="#">Forgot Password</a></li>
            </ul>
        </nav>
        </div>
    <div id="Content">
        <div id="PageHeading">
          <h1>Page Heading</h1>
        </div>
        <div id="ContentLeft">
        </div>
      <div id="ContentRight"></div>
    </div>
    <div id="Footer"></div>
    </div>
</body>
</html>

And here is the script that makes it:这是实现它的脚本:

<?php require 'Connections/Connections.php'; ?>
<?php

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

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

        $result = $con->query("select * from user where Username='$UN' AND     Password='$PW'");

        $row = $result->fetch_array(MYSQLI_BOTH);

        session_start();

        $_SESSION["UserID"] = $row['UserID'];

        header('Location: account.php');


    }






?>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
<link href="C:\Users\Mikkel\Desktop\HTML & CSS" rel="stylesheet" />
<link href="CSS/Layout.css" rel="stylesheet" type="text/css" />
<link href="CSS/Menu.css" rel="stylesheet" type="text/css" />
<link href="CSS/Bootstrap.css" rel="stylesheet" type="text/css" />
</head>
<body>
    <div id="Holder">
    <div id="Header"></div>
    <div id="NavBar">
        <nav>
            <ul>
                <li><a href="#">Login</a></li>
                <li><a href="#">Register</a></li>
                <li><a href="#">Forgot Password</a></li>
            </ul>
        </nav>
        </div>
    <div id="Content">
        <div id="PageHeading">
          <h1>Page Heading</h1>
        </div>
        <div id="ContentLeft">
          <h2>Your Message Here       </h2><br />
          <h6>Your Message</h6>
        </div>
      <div id="ContentRight">
        <form id="form1" name="form1" method="post">
            <div class="FormElement">
              <p>
                <input name="text" type="text" required="required" id="username" placeholder="Username">
              </p>
              <p>&nbsp;</p>
            </div>
            <div class="FormElement">
              <p>
                <input name="password" type="password" required="required" id="password" placeholder="Password">
              </p>
              <p>&nbsp;</p>
            </div>
            <div class="FormElement">
              <p>
                <input name="submit" type="submit" class="btn-primary" id="submit" value="Submit">
              </p>
              <p>&nbsp;</p>
            </div>
        </form>
      </div>
    </div>
    <div id="Footer"></div>
    </div>
</body>
</html>

It is usually best practices to start the session in the beginining of the document and not in the middle.通常最好的做法是在文档的开头而不是中间开始会话。 However, your issue is that when you make the query, it does not bring in the variables $UN and $PW.但是,您的问题是当您进行查询时,它不会引入变量 $UN 和 $PW。 Since you started it off with a " then, PHP will not parse the inside of it. You can "pause" it with: "select * from user where Username='" . $UN . "' AND Password='" . $PW . "'"由于您以"开头,因此 PHP 不会解析它的内部。您可以使用以下命令“暂停”它: "select * from user where Username='" . $UN . "' AND Password='" . $PW . "'"

So, the session is never being properly set.因此,会话永远不会被正确设置。 However, since the above method is unconventional, and since you are doing authentication, you should be using prepared statements.但是,由于上述方法是非常规的,并且由于您正在进行身份验证,因此您应该使用准备好的语句。 http://www.w3schools.com/php/php_mysql_prepared_statements.asp http://www.w3schools.com/php/php_mysql_prepared_statements.asp

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM