php 用户cookie登录验证与mysql数据登录验证的方法
发布:smiling 来源: PHP粉丝网 添加日期:2014-05-22 16:09:14 浏览: 评论:0
php用户cookie登录验证与mysql数据登录验证的方法:
- <?php
- unset($username);
- if ($_COOKIE['login']) {
- list($c_username,$cookie_hash) = split(',',$_COOKIE['login']);
- if (md5($c_username.$secret_word) == $cookie_hash) {
- $username = $c_username;
- } else {
- print "You have sent a bad cookie.";
- }
- }
- if ($username) {
- print "Welcome, $username.";
- } else {
- print "Welcome, anonymous user.";
- }
- ?>
看个完整的与数据库连接登录的代码:
- <html>
- <head>
- <title>Log-In Page</title>
- </head>
- <body>
- Please enter your user details to log-in here...
- <form action = "authenticate.php" method = "post">
- Username:<br>
- <input type = "text" name = "username">
- <br><br>
- Password:<br>
- <input type = "text" name = "password">
- <br><br>
- <input type = "submit" value = "Log In">
- </form>
- </body>
- </html>
File:authenticate.php
- <?php
- $username = $_POST['username'];
- $password = $_POST['password'];
- $self = $_SERVER['PHP_SELF'];
- $referer = $_SERVER['HTTP_REFERER'];
- if( ( !$username ) or ( !$password ) )
- { header( "Location:$referer" ); exit(); }
- $conn=@mysql_connect( "localhost", "userName", "password" ) or die( "Could not connect" );
- $rs = @mysql_select_db( "my_database", $conn ) or die( "Could not select database" );
- $sql = "select * from users where user_name="$username" and password = password( "$password" )";
- $rs = mysql_query( $sql, $conn ) or die( "Could not execute query" );
- $num = mysql_numrows( $rs );
- if( $num != 0 )
- {
- $msg = "<h3>Welcome $username - your log-in succeeded!</h3>";
- }
- else
- {
- header( "Location:$referer" ); exit();
- }
- ?>
- <html>
- <head>
- <title>Log-In Authenticated</title>
- </head>
- <body>
- <?php echo( $msg ); ?>
- </body>
- </html>
Tags: php 用户 cookie登录验证
相关文章
- ·PHP中通过Web 执行C/C++应用程序(2013-11-13)
- ·用PHP实现Ftp用户的在线管理(2013-11-13)
- ·用PHP自动把纯文本转换成Web页面(2013-11-13)
- ·用实例分析PHP5异常处理(2013-11-13)
- ·php5的simplexml解析错误(2013-11-13)
- ·PHP后门的隐藏技巧测试报告(2013-11-13)
- ·PHP缓存技术详谈(2013-11-27)
- ·利用PHP自定义错误处理器处理出错信息(2013-11-27)
- ·PHP作wap开发时遇到的问题(2013-11-27)
- ·php编写大型网站问题集(2013-11-27)
- ·php测试性能代码(2013-11-28)
- ·php 安全register globals设置为TRUE的危害(2013-11-28)
- ·XSLTProcessor 中 registerPHPFunctions 后无法调用 php 函数(2013-11-30)
- ·PHP中常用三种缓存技术(2013-11-30)
- ·新浪微博PHP版SDK的导致20007错误(2013-12-03)
- ·linux中phpMyAdmin错误提示Wrong permissions on configuration file, should no(2013-12-04)
推荐文章
热门文章
最新评论文章
- 写给考虑创业的年轻程序员(10)
- PHP新手上路(一)(7)
- 惹恼程序员的十件事(5)
- PHP邮件发送例子,已测试成功(5)
- 致初学者:PHP比ASP优秀的七个理由(4)
- PHP会被淘汰吗?(4)
- PHP新手上路(四)(4)
- 如何去学习PHP?(2)
- 简单入门级php分页代码(2)
- php中邮箱email 电话等格式的验证(2)