自己写的一个小小的php登陆模块

本文涉及的产品
云数据库 RDS MySQL Serverless,0.5-2RCU 50GB
简介:

今天写了一个小功能——PHP登陆 出来使用,才刚学PHP不久,有错误的地方请大虾们指正指正:

left.php           登陆页面

userlogin.php  验证功能

login.php         注销功能

config.auth.php 数据库连接功能

就四个页面,left.php通过post提交信息给userlog.php处理,userlog.php调用config.auth.php连接数据库进行数据处理,然后把结果返回left.php,logout.php就只是注销session功能。没有加入cookie,还暂时不需要保存cookie!代码如下:

left.php

 
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
  2. <html xmlns="http://www.w3.org/1999/xhtml"> 
  3. <head> 
  4. <meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> 
  5. <title>登陆系统</title> 
  6. </head> 
  7. <body> 
  8. <?php 
  9. session_start();  
  10. if ($_SESSION[username]) {  
  11. ?> 
  12. <form id="form3" name="form3" method="post" action=""> 
  13. <table width="150" border="1"> 
  14.     <tr> 
  15.       <td width="64">当前用户</td> 
  16.       <td width="70"><?php echo $_SESSION[username];?></td> 
  17.     </tr> 
  18.     <tr> 
  19.       <td colspan="2"><div align="center"><a href="logout.php">注销</a></div></td> 
  20.     </tr> 
  21.   </table> 
  22. </form> 
  23. <?php 
  24. }else {  
  25. ?> 
  26. <form id="form1" name="form1" method="post" action="userlogin.php"> 
  27.   <table width="150" border="1"> 
  28.     <tr> 
  29.       <td width="64">用户</td> 
  30.       <td width="70"><label> 
  31.         <input name="username" type="text" id="textfield" size="10" /> 
  32.       </label></td> 
  33.     </tr> 
  34.     <tr> 
  35.       <td>密码</td> 
  36.       <td><label> 
  37.         <input name="password" type="password" id="textfield2" size="10" /> 
  38.       </label></td> 
  39.     </tr> 
  40.     <tr> 
  41.       <td colspan="2"> 
  42.         <div align="center"> 
  43.           <input type="submit" name="submit" id="button" value="提交" /> 
  44.           <input type="reset" name="reset" id="button2" value="重置" /> 
  45.           </div></td> 
  46.     </tr> 
  47.   </table> 
  48. </form> 
  49. <?php 
  50. }  
  51. ?> 
  52. </body> 
  53. </html> 

config.php

 
  1. <?php  
  2. $dbhost="localhost";  
  3. $dbuser="root";  
  4. $dbpassword="123456";  
  5. $dbname="auth";  
  6. $conn=mysql_connect("$dbhost","$dbuser","$dbpassword"or die('不能连接服务器'.mysql_error());  
  7. mysql_select_db("$dbname",$connor die("数据库访问错误".mysql_errno());  
  8. mysql_query("set names gb2312");  
  9. ?> 

userlogin.php

 
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  2. <html xmlns="http://www.w3.org/1999/xhtml">  
  3. <head>  
  4. <meta http-equiv="Content-Type" content="text/html; charset=gb2312" />  
  5. <title>登陆系统</title>  
  6. </head>  
  7. <body>  
  8. <?php  
  9. session_start();  
  10. require("config.auth.php");  
  11. if ($_POST[submit]) {  
  12.     $sql="select * from userinfo where username = '$_POST[username]' and password = '$_POST[password]'";  
  13.     $result=mysql_query($sql);  
  14.     $numrows=mysql_num_rows($result);  
  15.     if ($numrows == 1) {  
  16.         $row=mysql_fetch_assoc($result);  
  17.         $_SESSION[username]=$row[username];  
  18.         //echo $_SESSION[username];  
  19.         echo "<script>alert('登陆成功!');window.location.href='left.php';</script>";  
  20.     }else {  
  21.         echo "<script>alert('登陆失败!');window.location.href='index.html';</script>";  
  22.     }  
  23. }else {  
  24.     echo "<script>alert('请通过正确途径登陆!');window.location.href='index.html';</script>";  
  25. }  
  26. mysql_free_result($result);  
  27. mysql_close($conn);  
  28. ?>  
  29. </body>  
  30. </html> 

logout.php

 
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  2. <html xmlns="http://www.w3.org/1999/xhtml">  
  3. <head>  
  4. <meta http-equiv="Content-Type" content="text/html; charset=gb2312" />  
  5. <title>登陆系统</title>  
  6. </head>  
  7. <body>  
  8. <?php  
  9.     session_start();  
  10.     unset($_SESSION[username]);  
  11.     session_destroy();  
  12.     echo "<script>alert('注销成功!');window.location.href='index.html';</script>";  
  13. ?>  
  14. </body>  
  15. </html> 

 本文转自运维笔记博客51CTO博客,原文链接http://blog.51cto.com/lihuipeng/597457如需转载请自行联系原作者


lihuipeng

相关实践学习
基于CentOS快速搭建LAMP环境
本教程介绍如何搭建LAMP环境,其中LAMP分别代表Linux、Apache、MySQL和PHP。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助 &nbsp; &nbsp; 相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
相关文章
|
6月前
|
PHP Apache
PHP环境搭建(配置php模块到apache服务器)
PHP环境搭建(配置php模块到apache服务器)
65 0
|
27天前
|
应用服务中间件 Linux PHP
Linux下安装php环境并且配置Nginx支持php-fpm模块
Linux下安装php环境并且配置Nginx支持php-fpm模块
29 0
|
3月前
|
SQL PHP 数据库
PHP案例:每一个账号登陆后的操作是隔离的(使用token进行登录)
PHP案例:每一个账号登陆后的操作是隔离的(使用token进行登录)
PHP案例:每一个账号登陆后的操作是隔离的(使用token进行登录)
|
8月前
|
关系型数据库 MySQL PHP
PHP注册、登陆、6套主页-带Thinkphp目录解析-【白嫖项目】
PHP注册、登陆、6套主页-带Thinkphp目录解析-【白嫖项目】 CSDN 转过来的,所以格式与内容有些许错误请见谅
|
9月前
|
PHP
PHP CURL模拟百度网盘登陆
PHP CURL模拟百度网盘登陆
|
9月前
thinkphp5.0 build.php自动创建模块目录和文件
thinkphp5.0 build.php自动创建模块目录和文件
52 0
|
11月前
|
PHP
PHP手术麻醉信息系统源码,术前管理模块功能
术前管理模块功能: 主要有手术排班、手术申请单、手术通知单、手术知情同意书、输血血液同意书、术前查房记录、术前访视、风险评估、手术计划等功能。
|
自然语言处理 NoSQL 关系型数据库
使用 Phpize 安装 PHP 的常用功能扩展模块 | 学习笔记
快速学习使用 Phpize 安装 PHP 的常用功能扩展模块
385 0
使用 Phpize 安装 PHP 的常用功能扩展模块 | 学习笔记
|
PHP 数据库
【PHP】连接数据库验证登陆
【PHP】连接数据库验证登陆
77 1