인터페이스:
<code><span><<span>html</span>></span><span><<span>head</span>></span><span><<span>title</span>></span>Login<span></<span>title</span>></span><span></<span>head</span>></span><span><<span>body</span>></span><span><<span>form</span><span>name</span>=<span>"fangbei"</span><span>method</span>=<span>"post"</span><span>action</span>=<span>"check_session_login.php"</span>></span><span><<span>div</span><span>style</span>=<span>"width:353"</span>></span><span><<span>dl</span>></span><span><<span>dt</span>></span><span></<span>dt</span>></span><span><<span>dd</span>></span><span><<span>div</span><span>align</span>=<span>"left"</span>></span> Username: <span><<span>input</span><span>type</span>=<span>"text"</span><span>name</span>=<span>"username"</span> /></span><span></<span>div</span>></span><span></<span>dd</span>></span><span><<span>dd</span>></span><span><<span>div</span><span>align</span>=<span>"left"</span>></span> Password : <span><<span>input</span><span>type</span>=<span>"password"</span><span>name</span>=<span>"pwd"</span> /></span><span></<span>div</span>></span><span></<span>dd</span>></span><span><<span>dd</span>></span><span><<span>p</span><span>align</span>=<span>"center"</span>></span><span><<span>input</span><span>type</span>=<span>"submit"</span><span>name</span>=<span>"Submit"</span><span>value</span>=<span>"Submit"</span> /></span><span><<span>input</span><span>type</span>=<span>"reset"</span><span>name</span>=<span>"Reset"</span><span>value</span>=<span>"Reset"</span> /></span><span></<span>p</span>></span><span></<span>dd</span>></span><span></<span>dl</span>></span><span></<span>div</span>></span><span></<span>form</span>></span><span></<span>body</span>></span><span></<span>html</span>></span></code>
데이터베이스 준비:
username | pwd | userflag |
perry | 123 | 1 |
로그인 확인:
<code><span><span><?php</span> header(<span>"Content-Type: text/html; charset=utf-8"</span>); <span>$servername</span>=<span>"localhost"</span>; <span>$username</span> = <span>"root"</span>; <span>$password</span> = <span>""</span>; <span>$dbname</span> = <span>"paul"</span>; <span>$conn</span> = <span>new</span> mysqli(<span>$servername</span>, <span>$username</span>, <span>$password</span>, <span>$dbname</span>); <span>if</span> (<span>$conn</span>->connect_error) { <span>die</span>(<span>"Connection failed: "</span> . <span>$conn</span>->connect_error); } <span>$conn</span>->query(<span>"SET NAMES utf8"</span>); <span>//获取输入的信息</span><span>$username</span> = <span>$_POST</span>[<span>'username'</span>]; <span>$pwd</span> = <span>$_POST</span>[<span>'pwd'</span>]; <span>//获取session的值</span><span>$sql</span> = <span>"select username,userflag from test_login where username = '$username' and pwd = '$pwd'"</span>; <span>$result</span> = <span>$conn</span>->query(<span>$sql</span>); <span>//判断用户以及密码</span><span>if</span> (<span>$result</span>->num_rows > <span>0</span>) { session_start(); <span>//判断权限</span><span>$row</span> = <span>$result</span>->fetch_assoc(); <span>if</span>(<span>$row</span>[<span>'userflag'</span>] == <span>1</span><span>or</span><span>$row</span>[<span>'userflag'</span>] == <span>0</span>){ <span>$_SESSION</span>[<span>'username'</span>] = <span>$row</span>[<span>'username'</span>]; <span>$_SESSION</span>[<span>'userflag'</span>] = <span>$row</span>[<span>'userflag'</span>]; <span>echo</span><span>"<a href='welcome_session_login.php'>欢迎访问</a>"</span>; } <span>else</span>{ <span>echo</span><span>"迷之用户"</span>; } } <span>else</span>{ <span>echo</span><span>"用户名或密码不正确"</span>; } </span></span></code>
환영 인터페이스:
<code><span><?php</span> header(<span>"Content-Type: text/html; charset=utf-8"</span>); session_start(); <span>if</span>(<span>isset</span>(<span>$_SESSION</span>[<span>'username'</span>])) { <span>if</span>(<span>$_SESSION</span>[<span>'userflag'</span>] == <span>1</span>) <span>echo</span><span>"欢迎管理员"</span>.<span>$_SESSION</span>[<span>'username'</span>].<span>"登录"</span>; <span>if</span>(<span>$_SESSION</span>[<span>'userflag'</span>] == <span>0</span>) <span>echo</span><span>"欢迎用户"</span>.<span>$_SESSION</span>[<span>'username'</span>].<span>"登录"</span>; } <span>else</span> { <span>echo</span><span>"登录已过期"</span>; } <span>unset</span>(<span>$_SESSION</span>[<span>'username'</span>]); <span>unset</span>(<span>$_SESSION</span>[<span>'pwd'</span>]); <span>unset</span>(<span>$_SESSION</span>[<span>'userflag'</span>]); <span>echo</span><span>"<br>会话结束"</span>;</code>
위 내용은 PHP 로그인 세션 확인 예시를 관련 내용과 함께 소개한 내용으로, PHP 튜토리얼에 관심이 있는 친구들에게 도움이 되기를 바랍니다.