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

时间:2013-10-25 23:14:13

标签: php session

所以我的网站有一个登录名,但是用户必须登录才能查看我不希望他们每次要添加会话时登录的每个页面,这样他们只需登录我需要在我的代码中添加的内容所以他们不需要登录,直到他们登出注销。 http://pastebin.com/dbE3mAKV

4 个答案:

答案 0 :(得分:1)

您可以从此处获取示例源代码,该代码将为您提供将其实施到您自己的网站上所需的一切http://www.sourcecodester.com/php/4947/login-logout-session-php-practice-beginners.html

答案 1 :(得分:1)

您可以使用session_start开始会话(必须在每个页面上调用才能运行)。有关会话的更多信息,请查看PHP.net的sessions page

答案 2 :(得分:1)

在您的登录页面上添加此内容;

 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!");    
 }  

答案 3 :(得分:1)

登录页面

<?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>

登录

                    <form action="#" name="loginform" method="post" >

                    <!--change input type to email if desired but then Spanish accented characters will fail validation -->
                    <label>Email </label>
                    <input type="text" class="styleinput" name="userEmail" maxlength="50" title="Enter Your email" autocomplete="off" placeholder="enter email" required/><br />

                    <label>Password </label>
                    <input type="password" class="styleinput" name="userPassword" maxlength="50" title="Enter Your Password" autocomplete="off" placeholder="enter password" required/>

                    <a href="help.php" class="stylelink"> Forgot your password? </a>
                    <input type="submit" name="Submit" class="stylesubmit" value="Submit">


                    </form>

</body>
</html>

用户必须登录的每个其他页面

<?php 
session_start(); 

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

您的代码 - 同时修改上述代码以适合您的网站,并且不要忘记在注册页面上加密密码。