Home > Backend Development > PHP Tutorial > PHP login session verification example

PHP login session verification example

WBOY
Release: 2016-07-29 08:57:13
Original
2676 people have browsed it

Interface:

<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>
Copy after login

Database preparation:

username pwd userflag
perry 123 1

Login verification:

<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>
Copy after login

Welcome interface:

<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>
Copy after login
').addClass('pre-numbering').hide(); $(this).addClass('has-numbering').parent().append($numbering); for (i = 1; i ').text(i)); }; $numbering.fadeIn(1700); }); });

The above introduces the PHP login session verification example, including the relevant content. I hope it will be helpful to friends who are interested in PHP tutorials.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template