php会话用户名/密码 [英] php sessions username/password

查看:43
本文介绍了php会话用户名/密码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我才刚刚开始学习 PHP,到目前为止还不错,但我对会话感到困惑.

如果有人能看看我的代码,让我知道我哪里出错了,我该如何修复它,那就太棒了!

用户名/密码按照说明硬编码到 admin 123.

谢谢

HTML

<头><meta http-equiv="Content-Type" content="text/html; charset=utf-8"/><title>if 语句</title>PHP<身体>登录<form action="username.php" method="post"><p><label for="username">输入您的用户名</label><input type="text" name="username" id="username"/></p><p><label for="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></表单></html><身体><?phpsession_start();如果($username=="admin"&&$password=="123")$username = isset($_POST['username']) ;$password = isset($_POST['password']) ;if ($name != 'admin' || $pass != '123'){elseif ($用户名 === '')die("错误:请输入用户名!");elseif ($password === '')die("错误:请输入密码!");elseif ($name == "test" && $pass == "test") {//认证成功,在会话中保存用户名:$_SESSION['user'] = 'admin';//重定向到 index.php 以欢迎登录用户:header('位置:index.html');出口;}别的 {die("错误:用户名或密码不正确!");}}?></html>

编辑

<身体><?phpsession_start();如果($用户名==管理员"&& $密码==123")$username = isset($_POST['username']) ;$password = isset($_POST['password']) ;if ($username == "admin" && $password == "123") {//认证成功,在会话中保存用户名:$_SESSION['用户名'] = '管理员';$_SESSION['密码'] = '123';//重定向到welcome.html 以欢迎登录用户:header('位置:welcome.html');出口;}别的 {die("错误:用户名或密码不正确!");}//没有提交数据,显示表单:?></html>

再次

<身体><?phpsession_start();如果(isset($_POST['用户名'],$_POST['密码'])){$username = $_POST['username'];$password = $_POST['password'];if($username === 'admin' && $password === '123')$_SESSION['用户名'] = '管理员';$_SESSION['密码'] = '123';session_write_close();{//重定向到welcome.html 以欢迎登录用户:header('位置:welcome.html');出口;}}别的 {die("错误:用户名或密码不正确!");}//没有提交数据,显示表单:?></html>

现在有点用了,但即使用户名/密码错误,它也会转到welcome.html

解决方案

这里有一个完整的例子,你想要的简单方法.首先 session_start().然后它会检查提交的用户名和密码.如果用户名是 admin 和密码是 pass 比它会去位置 wherever.php.如果用户名和密码错误,它会回显<?php echo $passError;?> 中的错误.您可以通过 <?php $variable=2; 在 php 中 echo 变量;echo $variable;?>.

 <头><meta http-equiv='content-type' content='text/html;charset=utf-8'/><title>登录</title><身体><表单名称='输入'动作=''方法='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'/></表单></html>

这包含在一个文件中,您也可以制作单独的两个文件,一个是 html,另一个是 php.如果您要制作两个文件,请在 <form action="" 中输入 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

解决方案

Here is a full example as you want in easy way.first the session_start().then it will check for submitted username and password.If username is admin and password is pass than it will go to location wherever.php.If username and password is wrong it will echo error in <?php echo $passError;?>.You can echo variable in php by <?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>

This included in one file you can also make separate two files,one html and other php.Enter php file location in <form action="" if you are making two files.

这篇关于php会话用户名/密码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆