简体   繁体   English

我在不使用session_start()的情况下在不同的php文件中使用了变量。 我对session_start()缺少什么,什么时候应该使用此函数?

[英]I use variables in different php files without using session_start(). What am I missing about session_start() and when should I use this function?

I am new to PHP. 我是PHP新手。 I am doing a little project (where I read data, a password, from a database) to learn PHP and I am using some variables that I obtain in a form in different PHP files. 我正在做一个小项目(从数据库中读取数据,密码,从中读取数据)以学习PHP,并且使用了一些以不同PHP文件的形式获取的变量。 I thought I should use session_start() and the global variable $_SESSION to use the same variables in different PHP files. 我以为我应该使用session_start()和全局变量$ _SESSION在不同的PHP文件中使用相同的变量。 Below it is my code, I have this files in one folder: index.php(HTML and forms), conn.php(connection to the database), login.php (file where I read data from database) 下面是我的代码,我将这些文件放在一个文件夹中:index.php(HTML和表格),conn.php(与数据库的连接),login.php(从数据库中读取数据的文件)

Can someone explain to me why I don't need to use session_start() in this case and in what specific situations I need to use the global variable $_SESSION? 有人可以向我解释为什么在这种情况下我不需要使用session_start(),在什么特定情况下需要使用全局变量$ _SESSION?

index.php: index.php文件:

<?php
include_once "conn.php";

?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>

    <form id="login" method="POST" action="login.php">
        <label for="loginUsername">Username</label>
        <input type="text" id="loginUsername" name="loginUsername">
        <br>
        <label for="loginPassword">Password</label>
        <input type="password" name="loginPassword" id="loginPassword"><br>
        <input type="submit" name="login" value="Login">
    </form>
</body>
</html>

conn.php: conn.php:

<?php
    $servername = "localhost";
    $username = "root";
    $password = "";
    $dbname = "loginsystem";

    $conn = mysqli_connect($servername, $username, $password, $dbname);

login.php: login.php中:

<?php
    include_once "conn.php";


    if(isset($_POST["login"])){

        $username = $_POST["loginUsername"];
        $password = $_POST["loginPassword"];

        echo $password;
        $sql = "SELECT password FROM users WHERE username=?;";

        $stmt = mysqli_stmt_init($conn);

        mysqli_stmt_prepare($stmt, $sql);
        mysqli_stmt_bind_param($stmt, 's', $username);
        mysqli_stmt_execute($stmt);

        $result = mysqli_stmt_get_result($stmt);
        while($row=mysqli_fetch_assoc($result)){
            echo $row["password"];
        }

    }

您需要首先使用session_start,在session_start之后只能使用$ _SESSION变量。我建议您编写config.php的第一行。

You don't need sessions to use a variable in different php files. 您不需要会话即可在其他php文件中使用变量。 However you can use sessions to store and access data from different requests. 但是,您可以使用会话来存储和访问来自不同请求的数据。

It could be the next step in your project. 这可能是您项目中的下一步。 After a user logs in, she might be authorized to do . 用户登录后,可能有权执行该操作。 You don't want her to login for every task. 您不希望她为每个任务登录。 So you need a way to allow her to perform those tasks without transmitting her password again. 因此,您需要一种允许她执行这些任务而无需再次发送密码的方法。

This is an example where sessions could help you (or you could solve it in a different way). 这是一个示例,会话可以帮助您(或者您可以通过其他方式解决它)。

But for the code you have now you don't need a session, it's fine as it is. 但是对于您现在拥有的代码,您不需要会话,就可以了。

if I understood you correctly you are wondering why you can access variables from one script in an other script. 如果我正确理解了您,您想知道为什么您可以从另一个脚本中的一个脚本访问变量。

The answer is simple: whenever you call include or require , everything in your included code - all variables and methods - are available in the caller script. 答案很简单:无论何时调用includerequire ,所包含代码中的所有内容(所有变量和方法)都可以在调用者脚本中找到。 This means that every single line of code of the included script is executed in the caller script. 这意味着所包含脚本的每一行代码都在调用者脚本中执行。

This means that you have to be carefully how you handle includes: if you define a function in a script and include the script twice, PHP will throw an error where it says that the function with this name is already defined. 这意味着您必须小心处理以下内容:如果在脚本中定义一个函数并两次包含该脚本,则PHP会在指出该名称的函数已定义的地方抛出错误。 To avoid this, you can use include_once or require_once . 为了避免这种情况,可以使用include_oncerequire_once

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

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