2016-09-11 51 views
-1

我只是剛剛開始學習PHP,並且目前爲止確實已經完成,但是我對會話感到困惑。php會話用戶名/密碼

如果有人可以看看我的代碼,並讓我知道我哪裏出錯了,我該如何解決這個問題太棒了!

按照說明將用戶名/密碼硬編碼到管理員123。

感謝

HTML

<!DOCTYPE> 
<html> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
<title>if statements</title> 
</head> 

PHP 

<body> 
Login 
<form action="username.php" method="post"> 
    <p> 
    <label for="username">Enter your username</label> 
    <input type="text" name="username" id="username" /> 
    </p> 
    <p> 
    <label for="password">Enter your password</label> 
    <input type="text" name="password" id="password" /> 
    </p> 
    <p> 
    <label for="GO"></label> 
    <input type="submit" name="GO" id="GO" value="Submit" /> 
    </p> 
</form> 
</body> 
</html> 



<html> 
<body> 

<?php 
session_start(); 
If($username==」admin」 && $password==」123」) 

    $username = isset($_POST['username']) ; 
    $password = isset($_POST['password']) ; 

    if ($name != 'admin' || $pass != '123') 
    { 
     elseif ($username === '') 
      die("ERROR: Please enter the username!"); 

     elseif ($password === '') 
      die("ERROR: Please enter the password!"); 

     elseif ($name == "test" && $pass == "test") { 

      // authentication successful, save username in session: 
      $_SESSION['user'] = 'admin'; 

      // redirect to index.php to welcome the logged in user: 
      header('Location: index.html'); 
      exit; 
     } 
     else { 
      die("ERROR: Incorrect username or password!"); 
     } 
    } 


    ?> 











</body> 
</html> 

編輯

<html> 
<body> 

<?php 
session_start(); 
If($username==」admin」 && $password=="123」) 

    $username = isset($_POST['username']) ; 
    $password = isset($_POST['password']) ; 



    if ($username == "admin" && $password == "123") { 

      // authentication successful, save username in session: 
      $_SESSION['username'] = 'admin'; 
      $_SESSION['password'] = '123'; 

      // redirect to welcome.html to welcome the logged in user: 
      header('Location: welcome.html'); 
      exit; 
     } 
     else { 
      die("ERROR: Incorrect username or password!"); 
     } 


    // no submitted data, display form: 
    ?> 

</body> 
</html> 

再次編輯:

<html> 
<body> 

<?php 


session_start(); 
if(isset($_POST['username'], $_POST['password'])) { 
    $username = $_POST['username']; 
    $password = $_POST['password']; 
    if($username === 'admin' && $password === '123') 
    $_SESSION['username'] = 'admin'; 
    $_SESSION['password'] = '123'; 

    session_write_close();{ 
        // redirect to welcome.html to welcome the logged in user: 
      header('Location: welcome.html'); 
      exit; 
    } 
} 


     else { 
      die("ERROR: Incorrect username or password!"); 
     } 


    // no submitted data, display form: 
    ?> 











</body> 
</html> 

這樣的工作了,但即使是錯誤的用戶名/密碼,它會welcome.html

+0

我怎麼會得到2拇指下來?我已經說過我剛剛開始學習php – Mikey

+0

我編輯了代碼,也許它更好一點,我可以得到一些幫助? – Mikey

+0

你的username.php文件的代碼在哪裏?你使用mysql數據庫來確認密碼嗎? – SreYash

回答

0

所以你的代碼有一些錯誤。

If($username==」admin」 && $password==」123」) 

$username = isset($_POST['username']) ; 
$password = isset($_POST['password']) ; 

您檢查是否$username$password是等於它們被定義之前的字符串。在檢查它是否等於某個字符串之前,你應該首先聲明你的變量。其次,isset函數將檢查變量是否已設置,如果是,則返回truefalse不是。第三,你在if語句後也忘記括號。

你應該做的是這樣的:

if(isset($_POST['username'], $_POST['password'])) { 
    $username = $_POST['username']; 
    $password = $_POST['password']; 
    if($username === 'admin' && $password === 'admin123') { 
     //code here 
    } 
} 
0

有些事情要考慮:

,然後輸出數據總是使用session_start()。

您在使用它們之後分配變量。這是錯誤的順序。 headering前

If($username==」admin」 && $password=="123」) 

    $username = isset($_POST['username']) ; 
    $password = isset($_POST['password']) ; 

使用session_write_close():

$_SESSION['password'] = '123'; 

session_write_close(); 

// redirect to welcome.html to welcome the logged in user: 
header('Location: welcome.html'); 

和閱讀您的代碼...你是在做同樣的,如果兩次。

0

下面是一個完整的例子,你在想方便的way.first。然後session_start()它會檢查提交的用戶名和用戶名password.If是admin和密碼是pass比它會去的位置。如果wherever.php用戶名和密碼是錯誤的,它會回顯錯誤<?php echo $passError;?>。你可以echo變量在php通過<?php $variable=2; echo $variable;?>

<?php 
    session_start(); 
$username = $password = $userError = $passError = ''; 
    if(isset($_POST['sub'])){ 
     $username = $_POST['username']; 
    $password = $_POST['password']; 
     if($username === 'admin' && $password === 'pass'){ 
     $_SESSION['login'] = true; 
    header('LOCATION:wherever.php'); //go to location after successful login. 
    die(); 
     } 
     if($username !== 'admin'){$userError = 'Invalid Username';} 
     if($password !== 'password'){$passError = 'Invalid Password';} 
    } 
    ?><!DOCTYPE html> 
    <html xmlns='http://www.w3.org/1999/xhtml' xml:lang='en' lang='en'> 
     <head> 
     <meta http-equiv='content-type' content='text/html;charset=utf-8' /> 
     <title>Login</title> 

     </head> 
    <body> 
     <form name='input' action='' method='post'> 
     <label for='username'></label><input type='text' value='' id='username' name='username' /> 
     <div class='error'><?php echo $userError;?></div> 
     <label for='password'></label><input type='password' value='' id='password' name='password' /> 
     <div class='error'><?php echo $passError;?></div> 
     <input type='submit' value='Home' name='sub' /> 
     </form> 

    </body> 
    </html> 

這包括在一個文件中,您還可以單獨兩個文件,一個HTML和其他php.Enter php文件位置<form action=""如果你正在兩個文件。

+0

謝謝,但這不是我想要的,我想通過與人一起工作,並試圖瞭解我錯了什麼地方,並不是每個人都想要簡單的答案。 – Mikey