Maison > développement back-end > tutoriel php > PHP登录session验证实例

PHP登录session验证实例

WBOY
Libérer: 2016-07-29 08:57:13
original
2677 Les gens l'ont consulté

界面:

<code><span>html</span>><span>head</span>><span>title</span>>Login<span><span>title</span>></span><span><span>head</span>></span><span>body</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>div</span><span>style</span>=<span>"width:353"</span>><span>dl</span>><span>dt</span>><span><span>dt</span>></span><span>dd</span>><span>div</span><span>align</span>=<span>"left"</span>>
       Username:
       <span>input</span><span>type</span>=<span>"text"</span><span>name</span>=<span>"username"</span> /><span><span>div</span>></span><span><span>dd</span>></span><span>dd</span>><span>div</span><span>align</span>=<span>"left"</span>>
       Password : 
       <span>input</span><span>type</span>=<span>"password"</span><span>name</span>=<span>"pwd"</span> /><span><span>div</span>></span><span><span>dd</span>></span><span>dd</span>><span>p</span><span>align</span>=<span>"center"</span>><span>input</span><span>type</span>=<span>"submit"</span><span>name</span>=<span>"Submit"</span><span>value</span>=<span>"Submit"</span> /><span>input</span><span>type</span>=<span>"reset"</span><span>name</span>=<span>"Reset"</span><span>value</span>=<span>"Reset"</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>
Copier après la connexion

数据库准备:

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>
Copier après la connexion

欢迎界面:

<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>;</span></code>
Copier après la connexion
').addClass('pre-numbering').hide(); $(this).addClass('has-numbering').parent().append($numbering); for (i = 1; i ').text(i)); }; $numbering.fadeIn(1700); }); });

以上就介绍了PHP登录session验证实例,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

Étiquettes associées:
source:php.cn
Déclaration de ce site Web
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn
Tutoriels populaires
Plus>
Derniers téléchargements
Plus>
effets Web
Code source du site Web
Matériel du site Web
Modèle frontal