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 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?