Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/227.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何检查用户登录并启动会话_Php_Session - Fatal编程技术网

Php 如何检查用户登录并启动会话

Php 如何检查用户登录并启动会话,php,session,Php,Session,因此,我的网站有一个登录名,但用户必须登录才能查看每个页面。我不希望他们在每次我想添加会话时登录,所以他们只登录一次。我需要在我的代码中添加什么,所以他们在点击“注销”之前不需要登录 您可以使用启动会话(必须在每个页面上调用才能工作)。有关会话的更多信息,请查看PHP.net的,您可以使用它启动会话(必须在每个页面上调用才能工作)。有关会话的更多信息,请查看PHP.net的,您可以使用它启动会话(必须在每个页面上调用才能工作)。有关会话的更多信息,请查看PHP.net的,您可以使用它启动会话(必

因此,我的网站有一个登录名,但用户必须登录才能查看每个页面。我不希望他们在每次我想添加会话时登录,所以他们只登录一次。我需要在我的代码中添加什么,所以他们在点击“注销”之前不需要登录

您可以使用启动会话(必须在每个页面上调用才能工作)。有关会话的更多信息,请查看PHP.net的

,您可以使用它启动会话(必须在每个页面上调用才能工作)。有关会话的更多信息,请查看PHP.net的

,您可以使用它启动会话(必须在每个页面上调用才能工作)。有关会话的更多信息,请查看PHP.net的

,您可以使用它启动会话(必须在每个页面上调用才能工作)。有关会话的更多信息,请查看登录页面上PHP.net的链接,添加此链接

 session_start();  
 $_SESSION['usename'] = $username;  
 $_SESSION['password'] = $password;  
在所有其他页面上添加以下内容:

 session_start();  

 $username = $_SESSION['usename'];  
 $password = $_SESSION['password'];  

 # Do database check on $username and $password  
 if($username =='test' AND $password =='pass1'){  
     echo "logged in";  
 } else {  
     die("please log in first!");    
 }  

在您的登录页面上添加此项

 session_start();  
 $_SESSION['usename'] = $username;  
 $_SESSION['password'] = $password;  
在所有其他页面上添加以下内容:

 session_start();  

 $username = $_SESSION['usename'];  
 $password = $_SESSION['password'];  

 # Do database check on $username and $password  
 if($username =='test' AND $password =='pass1'){  
     echo "logged in";  
 } else {  
     die("please log in first!");    
 }  

在您的登录页面上添加此项

 session_start();  
 $_SESSION['usename'] = $username;  
 $_SESSION['password'] = $password;  
在所有其他页面上添加以下内容:

 session_start();  

 $username = $_SESSION['usename'];  
 $password = $_SESSION['password'];  

 # Do database check on $username and $password  
 if($username =='test' AND $password =='pass1'){  
     echo "logged in";  
 } else {  
     die("please log in first!");    
 }  

在您的登录页面上添加此项

 session_start();  
 $_SESSION['usename'] = $username;  
 $_SESSION['password'] = $password;  
在所有其他页面上添加以下内容:

 session_start();  

 $username = $_SESSION['usename'];  
 $password = $_SESSION['password'];  

 # Do database check on $username and $password  
 if($username =='test' AND $password =='pass1'){  
     echo "logged in";  
 } else {  
     die("please log in first!");    
 }  
登录页面

<?php
session_start(); 

mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
$mysqli = new mysqli("localhost", "", "", "");

//if post and encryption
if(isset($_POST['Submit'])){

    $userEmail=strip_tags(trim($_POST['userEmail']));
    $userPassword=SHA1(trim($_POST['userPassword']));

    //Check to see if the user left any space empty! For browsers that do not support the HTML5 'required' atribute.
    if($userEmail==""||$userPassword=="") 
        { 
        $error="Please fill in all of the information"; 
        } else {


    if ($stmt = $mysqli->prepare("SELECT userID FROM users WHERE userEmail = ? and userPassword = ? LIMIT 1")) {
        $stmt->bind_param("ss", $userEmail, $userPassword);
        $stmt->execute();
        $stmt->store_result();
        if($stmt->num_rows > 0) {
            $_SESSION['Email'] = $userEmail;
            $stmt->close();
            //go to next page
            header ("location: nextpage.php");    
        }
        $error="Email and Password do not match, please try again."; 
    }
}}
?>

<!DOCTYPE html>
<head>
</head>

<body>
    <!--Display errors-->
<div class="errorbox">
    <?php if(isset($error)){?><strong class="error"><?php echo $error;?></strong><?php } ?>
</div>


登录

电子邮件

密码
用户必须登录的其他每一页

<?php 
session_start(); 

if(!isset($_SESSION['Email'])){
header("location:home.php");
}
?>

您的代码-也可以编辑以上代码以适合您的站点,并且不要忘记在注册页面上加密密码。

登录页面

<?php
session_start(); 

mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
$mysqli = new mysqli("localhost", "", "", "");

//if post and encryption
if(isset($_POST['Submit'])){

    $userEmail=strip_tags(trim($_POST['userEmail']));
    $userPassword=SHA1(trim($_POST['userPassword']));

    //Check to see if the user left any space empty! For browsers that do not support the HTML5 'required' atribute.
    if($userEmail==""||$userPassword=="") 
        { 
        $error="Please fill in all of the information"; 
        } else {


    if ($stmt = $mysqli->prepare("SELECT userID FROM users WHERE userEmail = ? and userPassword = ? LIMIT 1")) {
        $stmt->bind_param("ss", $userEmail, $userPassword);
        $stmt->execute();
        $stmt->store_result();
        if($stmt->num_rows > 0) {
            $_SESSION['Email'] = $userEmail;
            $stmt->close();
            //go to next page
            header ("location: nextpage.php");    
        }
        $error="Email and Password do not match, please try again."; 
    }
}}
?>

<!DOCTYPE html>
<head>
</head>

<body>
    <!--Display errors-->
<div class="errorbox">
    <?php if(isset($error)){?><strong class="error"><?php echo $error;?></strong><?php } ?>
</div>


登录

电子邮件

密码
用户必须登录的其他每一页

<?php 
session_start(); 

if(!isset($_SESSION['Email'])){
header("location:home.php");
}
?>

您的代码-也可以编辑以上代码以适合您的站点,并且不要忘记在注册页面上加密密码。

登录页面

<?php
session_start(); 

mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
$mysqli = new mysqli("localhost", "", "", "");

//if post and encryption
if(isset($_POST['Submit'])){

    $userEmail=strip_tags(trim($_POST['userEmail']));
    $userPassword=SHA1(trim($_POST['userPassword']));

    //Check to see if the user left any space empty! For browsers that do not support the HTML5 'required' atribute.
    if($userEmail==""||$userPassword=="") 
        { 
        $error="Please fill in all of the information"; 
        } else {


    if ($stmt = $mysqli->prepare("SELECT userID FROM users WHERE userEmail = ? and userPassword = ? LIMIT 1")) {
        $stmt->bind_param("ss", $userEmail, $userPassword);
        $stmt->execute();
        $stmt->store_result();
        if($stmt->num_rows > 0) {
            $_SESSION['Email'] = $userEmail;
            $stmt->close();
            //go to next page
            header ("location: nextpage.php");    
        }
        $error="Email and Password do not match, please try again."; 
    }
}}
?>

<!DOCTYPE html>
<head>
</head>

<body>
    <!--Display errors-->
<div class="errorbox">
    <?php if(isset($error)){?><strong class="error"><?php echo $error;?></strong><?php } ?>
</div>


登录

电子邮件

密码
用户必须登录的其他每一页

<?php 
session_start(); 

if(!isset($_SESSION['Email'])){
header("location:home.php");
}
?>

您的代码-也可以编辑以上代码以适合您的站点,并且不要忘记在注册页面上加密密码。

登录页面

<?php
session_start(); 

mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
$mysqli = new mysqli("localhost", "", "", "");

//if post and encryption
if(isset($_POST['Submit'])){

    $userEmail=strip_tags(trim($_POST['userEmail']));
    $userPassword=SHA1(trim($_POST['userPassword']));

    //Check to see if the user left any space empty! For browsers that do not support the HTML5 'required' atribute.
    if($userEmail==""||$userPassword=="") 
        { 
        $error="Please fill in all of the information"; 
        } else {


    if ($stmt = $mysqli->prepare("SELECT userID FROM users WHERE userEmail = ? and userPassword = ? LIMIT 1")) {
        $stmt->bind_param("ss", $userEmail, $userPassword);
        $stmt->execute();
        $stmt->store_result();
        if($stmt->num_rows > 0) {
            $_SESSION['Email'] = $userEmail;
            $stmt->close();
            //go to next page
            header ("location: nextpage.php");    
        }
        $error="Email and Password do not match, please try again."; 
    }
}}
?>

<!DOCTYPE html>
<head>
</head>

<body>
    <!--Display errors-->
<div class="errorbox">
    <?php if(isset($error)){?><strong class="error"><?php echo $error;?></strong><?php } ?>
</div>


登录

电子邮件

密码
用户必须登录的其他每一页

<?php 
session_start(); 

if(!isset($_SESSION['Email'])){
header("location:home.php");
}
?>

您的代码-也可以编辑以上代码以适合您的站点,并且不要忘记在注册页面上加密密码