2016-12-14 54 views
-4

實際使用的「$ _SESSION [‘AUTHUSER’] = 1」的宗旨,我無法理解爲什麼我們用「$ _SESSION [‘AUTHUSER’] = 1;」在php代碼中,我的代碼如下什麼是PHP,爲什麼我們在本節用1

<?php 
session_start(); 
$_SESSION['username'] = $_POST['user']; 
$_SESSION['userpass'] = $_POST['pass']; 
$_SESSION['authuser'] = 1; 
//Check username and password information 

if(($_SESSION['username'] == 'joe') and 
($_SESSION['userpass'] == '123')) { 
$_SESSION['authuser'] = 1; 
} 
else 
{ 
echo 'Sorry, but you don\'t have permission to view this page!'; 
exit(); 
} 

?> 

回答

1

因爲會話(和cookie)支持需要它,原因很多。

即,否則將需要你(和你的訪問者),輸入用戶名和密碼,當你在你的頁面的任何鏈接點擊每一次。

<?php 
    session_start(); 
    $_SESSION['username'] = $_POST['user']; 
    $_SESSION['userpass'] = $_POST['pass']; 
    $_SESSION['authuser'] = 0; // user is not authenticated (just a GUEST), default is 0... 


// if visitor is priviledged, show him in, let him see the page 

    if(($_SESSION['username'] == 'joe') and 
    ($_SESSION['userpass'] == '123')) { 
    $_SESSION['authuser'] = 1; // insert 1 into DB and set cookie as 1 for user not to enter username and pswd anymore during browsing 
    } 
    else 
    { 
//else, keep guest away from a page 
    echo 'Sorry, but you don\'t have permission to view this page!'; 
    exit(); // shut down 
    } 

    ?> 
1

在你的情況SESSION的用戶名和爲userpass的使用似乎是多餘的。這是可能的。

<?php 
session_start(); 
/*Do not set sessions for username and userpass, only use them in the POST array 
*Initialize authuser to 0 because by default a user is not logged in 
*/ 
$_SESSION['authuser'] = 0; 
//Check username and password information 
if(($_POST['user'] == 'joe') and 
    ($_POST['pass'] == '123')) { //Check the user and set it as authenticated 
    $_SESSION['authuser'] = 1; 
} else { //If the user is not valid, stop execution 
    echo 'Sorry, but you don\'t have permission to view this page!'; 
    exit(); 
} 
?> 

什麼我在這裏做的是:

  • 啓動會議
  • 初始化用戶不認證(可選的)
  • 檢查用戶名和密碼
    • 如果他們如認證
    • 如果無效,設置用戶,STO pping執行。

注意,這可能是一次用戶進行身份驗證的用戶名和密碼設置會話有用,而不是隻記住用戶登錄。

相關問題