php会话用户名/密码 [英] php sessions username/password
问题描述
我才刚刚开始学习 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'/></表单>