简体   繁体   English

PHP:$ _SESSION未设置

[英]PHP: $_SESSION doesn't set

Before setting as duplicate, I've spent 4 hours on researching about my problem, but I had no luck. 在设置为重复副本之前,我花了4个小时来研究自己的问题,但我没有运气。

I am trying to make a signup/login system for my website. 我正在尝试为我的网站建立注册/登录系统。 The main point that doesn't seem to work is that when I am signing up on my website, the session doesn't seem to start. 似乎无效的要点是,当我在网站上注册时,会话似乎没有开始。 The reason that I can see it is because, on my navbar, I have set it to change from signup to log out. 之所以可以看到它,是因为在我的导航栏中,我已将其设置为从注册更改为注销。 Here is the piece of code for that: 这是该段代码:

<ul>
    <li class="list1"><a href="GeorgeKarabassis.php">Home</a></li>
    <li class="list2"><a href="about.php">About</a></li>
    <li class="list3"><a href="portfolio.php">Portfolio</a></li>
    <li class="list4"><a href="Blog.php">Blog</a></li>
    <li class="list4"><a href="contact_us.php">Contact</a></li>
    <?php
        if (isset($_SESSION['id'])){
            echo "<li><a href='#'>SIGN OUT</a></li>";
        }
        else{
            echo "<li><a onclick='signup(event)' href='#'>SIGN UP</a></li>";
        }
    ?>
</ul>

To make that I have created three files. 为此,我创建了三个文件。 One is the mane page, one is the signup file itself, code below: 一个是鬃毛页面,一个是注册文件本身,代码如下:

<?php
    session_start();
    include "../dbh.php";

    $first = $_POST["first"];
    $last = $_POST["last"];
    $uid = $_POST["uid"];
    $email = $_POST["email"];
    $pwd = $_POST["pwd"];

    $sql = "INSERT INTO users (first,last,uid,email,pwd) VALUES ('$first','$last','$uid','$email','$pwd')";
    $result = mysqli_query($conn, $sql);
    $row = mysqli_fetch_assoc($result);
    $_SESSION['id'] = $row['id'];
    header("Location: ../index.php");
    exit();

and the last one is the file which connects PHP to the database code below: 最后一个是将PHP连接到下面的数据库代码的文件:

$conn = mysqli_connect("XXX","XXX","XXX","XXX");

if (!$conn){
    die("Connection failed: ".mysqli_connect_error());
}

I believe that the session doesn't start because the main page reloads after the user hits signup on the form, but I have started the session on all of my files (except the database connection file where it's not needed). 我相信该会话不会启动,因为在用户点击表单上的注册后,主页会重新加载,但是我已经在所有文件(不需要数据库连接文件的情况下)上启动了该会话。 I used session start on all of my page and I placed it on the beginning of all pages with opening and closing PHP tags. 我在所有页面上都使用了会话开始,并通过打开和关闭PHP标签将其放置在所有页面的开始处。

Any suggestions? 有什么建议么? I appreciate your answers and comments! 感谢您的回答和评论!

Sorry for the bad English but it's not my first language. 对不起,英语不好,但这不是我的母语。

This: 这个:

$sql = "INSERT INTO users (first,last,uid,email,pwd) VALUES ('$first','$last','$uid','$email','$pwd')";
$result = mysqli_query($conn, $sql);
$row = mysqli_fetch_assoc($result);
           ^^^^^^^^^^^^^^^^^^
$_SESSION['id'] = $row['id'];

Insert queries do NOT return a result set, and you can NOT fetch() from them. 插入查询不会返回结果集,也无法从中fetch() That means mysqli_fetch_assoc() is failing, and returning a boolean FALSE. 这意味着mysqli_fetch_assoc()失败,并返回布尔FALSE。 You then use that boolean false as if it was an array, and are basically doing the equivalent of 然后,您可以使用该布尔值false,就好像它是一个数组一样,并且基本上在做与

$_SESSION['id'] = null;

Note this: 请注意:

php > $foo = false;
php > $id = $foo['id'];
php > var_dump($id);
NULL

You want 你要

$_SESSION['id'] = mysqli_insert_id($conn);

instead. 代替。

It is an error with you SQL query. 您的SQL查询错误。

$sql = "INSERT INTO users (first,last,uid,email,pwd) VALUES ('$first','$last','$uid','$email','$pwd')";
    $result = mysqli_query($conn, $sql);
    $row = mysqli_fetch_assoc($result);

The first line of the code is an INSERT command. 代码的第一行是INSERT命令。 The second line executes this command by sending it to the server. 第二行通过将其发送到服务器来执行此命令。 If query is properly processed then MySQL server doesn't return you anything, so $result will equal to true . 如果查询得到正确处理,则MySQL服务器不会返回任何信息,因此$result将等于true It wil not contain any data from the database . 它不会包含数据库中的任何数据 So you can't fetch it, what you try to do in the third line. 因此,您无法获取它,这是您尝试在第三行中执行的操作。 Need to make a separate query for data. 需要单独查询数据。

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

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