doushih06137 2016-09-11 11:39
浏览 41

php会话用户名/密码

I'm only just starting to learn PHP and have done ok so far but I have confused myself with sessions.

If someone could have a look at my code and let me know where i have went wrong and how i can fix it that would be fantastic!

The username/password is hard coded to admin 123 as instructed.

Thanks

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>

EDIT

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

EDIT AGAIN:

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

It kind of works now, but even with wrong username/password it goes to welcome.html

  • 写回答

3条回答 默认 最新

  • duanlvxing7707 2016-09-11 12:03
    关注

    So there are a few things wrong with your code.

    If($username==”admin” && $password==”123”)
    
    $username = isset($_POST['username']) ;
    $password = isset($_POST['password']) ;
    

    You are checking if $username and $password are equal to a string before they are defined. You should first declare your variable before checking if it's equal to some string. Second, the isset function will check if the variable is set and then return true if so and false is it isn't. Third you are also forgetting brackets after the if statement.

    You should do it like this:

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

报告相同问题?

悬赏问题

  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序