php会话用户名/密码

时间:2016-09-11 11:39:03

标签: php session

我刚刚开始学习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

3 个答案:

答案 0 :(得分:0)

因此您的代码存在一些问题。

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

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

您正在检查$username$password在定义之前是否等于字符串。在检查变量是否等于某个字符串之前,应首先声明变量。其次,isset函数将检查变量是否已设置,如果是,则返回true,而false则不然。第三,在if语句之后你也忘记括号。

你应该这样做:

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

答案 1 :(得分:0)

需要考虑的一些事项:

在输出数据之前始终使用session_start()。

您在使用后分配变量。这是错误的订单。

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');

阅读你的代码......如果两次,你也会这样做。

答案 2 :(得分:0)

以下是您想要的完整示例。首先是session_start()。然后它将检查提交的用户名和密码。如果用户名是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 <form action=""中的php文件位置,如果你要制作两个文件。