doudang1890 2018-08-15 22:10
浏览 71
已采纳

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

I am new to 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. I thought I should use session_start() and the global variable $_SESSION to use the same variables in different PHP files. 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)

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?

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:

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

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

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"];
        }

    }
  • 写回答

3条回答 默认 最新

  • doupang9080 2018-08-15 22:45
    关注

    You don't need sessions to use a variable in different php files. 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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 seatunnel-web使用SQL组件时候后台报错,无法找到表格
  • ¥15 fpga自动售货机数码管(相关搜索:数字时钟)
  • ¥15 用前端向数据库插入数据,通过debug发现数据能走到后端,但是放行之后就会提示错误
  • ¥30 3天&7天&&15天&销量如何统计同一行
  • ¥30 帮我写一段可以读取LD2450数据并计算距离的Arduino代码
  • ¥15 飞机曲面部件如机翼,壁板等具体的孔位模型
  • ¥15 vs2019中数据导出问题
  • ¥20 云服务Linux系统TCP-MSS值修改?
  • ¥20 关于#单片机#的问题:项目:使用模拟iic与ov2640通讯环境:F407问题:读取的ID号总是0xff,自己调了调发现在读从机数据时,SDA线上并未有信号变化(语言-c语言)
  • ¥20 怎么在stm32门禁成品上增加查询记录功能