在PHP上使用会话

在PHP上使用会话,php,session,localhost,Php,Session,Localhost,我是PHP编程新手,我有用户登录代码,并且已经连接到localhost数据库 下面是Login.php的代码 <html> <head> <title>Login Page</title> </head> <body> <div> <form action="doLogin.php" method="POST"> <p>

我是PHP编程新手,我有用户登录代码,并且已经连接到localhost数据库

下面是Login.php的代码

<html>
<head>
    <title>Login Page</title>
</head>
<body>
    <div>
        <form action="doLogin.php" method="POST">
            <p>
                <label>Username : </label>
                <input type="text" id="username" name="user">
            </p>
            <p>
                <label>Password : </label>
                <input type="text" id="password" name="pass">
            </p>
            <p>
                <input type="submit" id="btnLogin" value="Login">
            </p>
        </form>
    </div>
</body>
</html>

登录页面

用户名:

密码:

doLogin.php

<?php
    $username = $_POST["user"];
    $password = $_POST["pass"];

    $username = stripcslashes($username);
    $password = stripcslashes($password);

    $con = mysqli_connect('localhost', 'root', '', 'dbtest');

    $query = mysqli_query($con, "select * from user where Username = '$username' and Password = '$password' ");
    $row = mysqli_fetch_array($query);

    if ($row['Username'] == $username && $row['Password'] == $password)
    {
        echo "You are Success Login!!! Welcome ".$row['Username'];
        header('Location: Profile.php');
    }
    else
    {
        echo "Failed to login!";
    }
?>


我想使用
会话
,因此如果用户未登录,它将重定向到登录页面。我在哪里以及如何将会话应用到代码中,非常感谢。

首先,您需要调用
session\u start()
,如果您没有通过index.php或类似的方式包含部分页面,则需要在每个页面或每个文件中调用
session\u start()
。确保调用
session_start()
在文件顶部,就在
之后,在开始时使用session_start(),当用户通过身份验证后,将用户名存储在会话变量中

<?php
    session_start(); //starting the session
    $username = $_POST["user"];
    $password = $_POST["pass"];

    $username = stripcslashes($username);
    $password = stripcslashes($password);

    $con = mysqli_connect('localhost', 'root', '', 'dbtest');

    $query = mysqli_query($con, "select * from user where Username = '$username' and Password = '$password' ");
    $row = mysqli_fetch_array($query);

    if ($row['Username'] == $username && $row['Password'] == $password)
    {
        echo "You are Success Login!!! Welcome ".$row['Username'];
        $_SESSION['username'] = $username; //Storing username in global variable
        header('Location: Profile.php');
    }
    else
    {
        echo "Failed to login!";
    }
?>

我做了一些搜索,并实现了这段代码

我创建了
session.php
文件,下面是代码

<?php
    function checkSession()
    {
        if(!isset($_SESSION))
        {
            session_start();
        }
        $username=$_SESSION['Username'];
        if(empty($username))
        {
            return true;
        }
        return false;
    }
?>

我将保护未经授权用户的每个页面的代码放在代码的顶部

<?php
    require_once("session.php");
    if(checkSession())
    {
        header('Location: Login.php');
        exit;
    }
    else
    {
?>
        //User Logged in...
<?php
    }
?>

//用户已登录。。。

希望这能帮助其他对PHP编程有同样问题的新手。

在顶部。会话_start();你可以用谷歌搜索一下。也不要直接将输入数据发布到查询中。阅读更多关于PHP中Santizing输入的信息。另外,请阅读更多关于在php中使用准备好的语句的内容。该代码可以完美地防止用户进入需要用户首先登录的页面。但当用户想要登录时,它会不断返回登录页面。我已将此代码添加到我要保护的页面
<?php
    function checkSession()
    {
        if(!isset($_SESSION))
        {
            session_start();
        }
        $username=$_SESSION['Username'];
        if(empty($username))
        {
            return true;
        }
        return false;
    }
?>
<?php
    require_once("session.php");
    if(checkSession())
    {
        header('Location: Login.php');
        exit;
    }
    else
    {
?>
        //User Logged in...
<?php
    }
?>