当前位置: 移动技术网 > IT编程>开发语言>PHP > php实现登陆模块功能示例

php实现登陆模块功能示例

2017年12月12日  | 移动技术网IT编程  | 我要评论

本文实例讲述了php实现登陆模块功能的方法。分享给大家供大家参考,具体如下:

最近在学习php。学了一点关于登陆的东西,写下来备忘。

新建四个页面,分别命名为:
login.php
check.php
index.php
error.php

login页面用表单建立一个登陆页面,不多说了。在代码里用js脚本判断用户名和密码不能为空,为空则重置焦点。代码如下:

<script type="text/javascript">
function jc()
{
 var username=document.getelementbyid("username");
 var userpwd=document.getelementbyid("userpwd");
 if(username.value=="")
 {
 alert("请输入用户名");
 username.focus();
 return false;
 }
 if(userpwd.value=="")
 {
 alert("请输入用户名");
 userpwd.focus();
 return false;
 }
}
</script>

check是检查页面,如果密码和用户名正确则重定向到index.php,否则定向到错误页面。代码如下:

<? session_start();
 $username=$_post["username"];
 $userpwd=$_post["userpwd"];
 if($username=="admin"&&$userpwd=="123456")
 {
 $_session["username"]=$username;
 echo "<script type='text/javascript'>window.location='index.php';
</script>";
 }
 else
 {
 echo"<script type='text/javascript'>
window.location='error.php';
</script>";
 }
?>

最后说说session验证。session函数是php自带的函数,用于记录用户的登录信息,类似于cookie,但又有所区别。

我们可以在验证页面定义和使用session,然后在首页再次定义和使用,以达到欢迎莫某的效果。上面再检查里的代码已经有了,下面是首页里的代码:

<?
session_start();
?>
<!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en"
"http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
</head>
<body>
欢迎<? echo $_session["username" ]; ?>来到这里
</body>
</html>

验证一下,登陆页面输入用户名和密码,如果正确,会跳到首页,显示欢迎某某某,如果错误会跳到错误页面,显示错误。

更多关于php相关内容感兴趣的读者可查看本站专题:《php+mysql数据库操作入门教程》、《php基本语法入门教程》、《php运算与运算符用法总结》、《php面向对象程序设计入门教程》、《php网络编程技巧总结》、《php数组(array)操作技巧大全》、《php字符串(string)用法总结》及《php常见数据库操作技巧汇总

希望本文所述对大家php程序设计有所帮助。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网