php注册登录?开发者账号注册
大家好,关于php注册登录很多朋友都还不太明白,今天小编就来为大家分享关于开发者账号注册的知识,希望对各位有所帮助!
如何用php做出登陆注册留言板
登录页:login.php
<?php
include("conn.php");
$username=$_POST['name'];
$password=$_POST['password'];
$yanzheng=$_POST['yanzheng'];
if(isset($_POST['submit']))
{
$sql=("select username,password from member where username='$username' and password='$password'") or die("sql语句执行失败");
//print_r($sql);
$ar=mysql_query($sql);
if($ar)
{
if($row=mysql_fetch_array($ar))
{
session_start();
if($_POST["yanzheng"])
{
if($yanzheng!=$_session[pic]||$yanzheng=="")
{
echo"验证码输入有误";
exit;
}
if($yanzheng==$_session[pic])
{
header("location:index.php");
}
}
}
else
{
echo"用户名或密码错误";
}
}
}
?>
<form action="login.php" method="post">
<table border=1 align=center width=500 height=300 bgColor=#DFFFDF bordercolor=#fffbec>
<tr>
<td colspan=2 align=center>用户登录</td>
</tr>
<tr>
<td>用户姓名:</td>
<td><input type="text" name="name" id="name"/></td>
</tr>
<tr>
<td>用户密码:</td>
<td><input type="password" name="password" id="password"/></td>
</tr>
<tr>
<td>验证码:</td>
<td><input type="text" name="yanzheng" id="yanzheng"/>
<img src="yanzheng1.php" width="50" height="30"></img>
</td>
</tr>
<tr>
<td colspan=3 align=center>
<input type="submit" name="submit" value="登录"/>
<input type="reset" name="reset" value="重置"/>
<a rel="external nofollow" href="register.php">注册</a>
</td>
</tr>
</table>
</form>
注册页:register.php
<?php
include("conn.php");
if(isset($_POST['submit'])&&$_POST['submit']){
if($_POST['username']=='')
{
echo"用户名不能为空";
exit();
}
if($_POST['password']=='')
{
echo"密码不能为空";
exit();
}
if($_POST['realpass']!=$_POST['password'])
{
echo"两次密码输入不一致";
exit();
}
$sql="insert into member(username,real_name,password,email,headimg) values('$_POST[username]','$_POST[username]','$_POST[password]','$_POST[email]','')";
$ar=mysql_query($sql);
if($ar)
{
header("location:index.php");
}
else
{
echo mysql_error();
}
}
?>
<body>
<form action="register.php" method="post">
<table border=1 align=center width=500>
<tr>
<td height=40 bgColor=#DFFFDF colspan=2>会员注册 [<a rel="external nofollow" rel="external nofollow" href="login.php">返回登录页</a>]</td>
</tr>
<tr>
<td height=40 bgColor=#fffbec>会员ID</td>
<td><input type="text" name="username" id="username"/></td>
</tr>
<tr>
<td height=40 bgColor=#fffbec>密码</td>
<td><input type="password" name="password" id="password"/></td>
</tr>
<tr>
<td height=40 bgColor=#fffbec>确认密码</td>
<td>
<input type="password" name="realpass" id="realpass"/>
</td>
</tr>
<tr>
<td height=40 bgColor=#fffbec>EMAIL</td>
<td><input type="text" name="email" id="email"/>
</tr>
<tr>
<td height=40 bgColor=#fffbec></td>
<td><input type="submit" name="submit" value="注册"/><input type="reset" value="重置"></td>
</tr>
</table>
</form>
</body>
主页显示:index.php
<?php
include("conn.php");
function cutstr($str,$cutleng)
{
$str=$str;//要截取的字符串
$cutleng=$cutleng;//要截取的长度
$strleng= strlen($str);//字符串长度
if($cutleng>$strleng)return$str;//字符串长度小于规定字数时,返回字符串本身
$notchinanum= 0;//初始不是汉字的字符数
for($i=0;$i<$cutleng;$i++)
{
if(ord(substr($str,$i,1))<=128)
{
$notchinanum++;
}
}
if(($cutleng%2==1)&&($notchinanum%2==0))//如果要截取奇数个字符,所要截取长度范围内的字符必须含奇数个非汉字,否则截取的长度加一
{
$cutleng++;
}
if(($cutleng%2==0)&&($notchinanum%2==1))//如果要截取偶数个字符,所要截取长度范围内的字符必须含偶数个非汉字,否则截取的长度加一
{
$cutleng++;
}
return substr($str,0,$cutleng);
}
?>
<html>
<head>
<script type="text/javascript">
function All(e, itemName)
{
var aa= document.getElementsByName(itemName);
for(var i=0; i<aa.length; i++)
aa[i].checked= e.checked;//得到那个总控的复选框的选中状态
}
function Item(e, allName)
{
var all= document.getElementsByName(allName)[0];
if(!e.checked) all.checked= false;
else
{
var aa= document.getElementsByName(e.name);
for(var i=0; i<aa.length; i++)
if(!aa[i].checked) return;
all.checked= true;
}
}
</script>
</head>
<?php
include("conn.php");
if(isset($_POST['del']))
{
$mm=$_POST["selected"];
$id=implode(",",$mm);
$sql="delete from forums where id in(".$id.")";
//echo$sql;
$result=mysql_query($sql);
echo$result?"删除成功":"删除失败";
}
?>
<table style="BORDER-BOTTOM-WIDTH: 1px; BORDER-COLLAPSE: collapse" cellSpacing=0 cellPadding=0 width=600 align=center border=1 bordercolor=#ddddff>
<tr align=middle>
<td height=40 bgColor=#DFFFDF colspan=3>论坛列表</td>
</tr>
<tr>
<td colspan=3><a rel="external nofollow" rel="external nofollow" href="login.php" style="float:right">[退出系统]</a><a rel="external nofollow" href="add_forum.php" style="float:right">[添加论坛]</a></td>
<td></td>
</tr>
<tr align=middle>
<td height=40 bgColor=#DFFFDF width=80>状态</td>
<td height=40 bgColor=#DFFFDF>论坛</td>
<td height=40 bgColor=#DFFFDF>最后更新</td>
</tr>
<?php
$sql="select* from forums";
$result=mysql_query($sql);
$num=mysql_num_rows($result);
if($num>0)
{
while($row=mysql_fetch_array($result)){
?>
<tr align=middle>
<td bgColor=#fffbec><input type="checkbox" name="selected" value="1"/></td>
<td height=50 bgColor=#fffbec width=300>
<?php
echo"<div><a href=\"forums.php?F=".$row['ID']."\">".$row['forum_name']."</a></div>";
echo cutstr($row['forum_description'],24);//最多显示24个字节,12个字,多余部分用省略号代替
echo"……";
?>
</td>
<td height=50 bgColor=#fffbec><div><?php echo$row['last_post_time']."by".$row['last_post_author']?></div></td>
</tr>
<?php
}
}
else
{
echo"<tr bgColor=#fffbec><td colspan=3>对不起,论坛尚在创建中……</td></tr>";
}
?>
<tr>
<td colspan=3><input type="checkbox" name="selected" value="1" onclick="All(this,'selected')"/>全选/不全选</td>
</tr>
<tr>
<td><input type="button" name="del" id="del" value="删除选中项"/>
<?php
?>
</td>
</tr>
</table>
</html>
自己创建数据库就好。
用php的CI框架怎么写登录和注册
第一步:login.php
//登陆方法
public function login(){
//如果用户名和密码为空,则返回登陆页面
if(empty($_POST['username'])|| empty($_POST['password'])){
$data['verifycode']= rand(1000,9999);//生成一个四位数字的验证码
//将验证码放入session中,注意:参数是数组的格式
$this->session->set_userdata($data);
//注意:CI框架默认模板引擎解析的模板文件中变量不需要$符号
//$this->parser->parse("admin/login",$data);
//smarty模板变量赋值
$this->tp->assign("verifycode",$data['verifycode']);
//ci框架在模板文件中使用原生态的PHP语法输出数据
//$this->load->view('login',$data);//登陆页面,注意:参数2需要以数组的形式出现
//显示smarty模板引擎设定的模板文件
$this->tp->display("admin/login.php");
}else{
$username= isset($_POST['username'])&&!empty($_POST['username'])?trim($_POST['username']):'';//用户名
$password= isset($_POST['password'])&&!empty($_POST['password'])?trim($_POST['password']):'';//密码
$verifycode= isset($_POST['verifycode'])&&!empty($_POST['verifycode'])?trim($_POST['verifycode']):'';//验证码
//做验证码的校验
if($verifycode==$this->session->userdata('verifycode')){
//根据用户名及密码获取用户信息,注意:参数2是加密的密码
$user_info=$this->user_model->check_user_login($username,md5($password));
if($user_info['user_id']> 0){
//将用户id、username、password放入cookie中
//第一种设置cookie的方式:采用php原生态的方法设置的cookie的值
//setcookie("user_id",$user_info['user_id'],86500);
//setcookie("username",$user_info['username'],86500);
//setcookie("password",$user_info['password'],86500);
//echo$_COOKIE['username'];
//第二种设置cookie的方式:通过CI框架的input类库
$this->input->set_cookie("username",$user_info['username'],3600);
$this->input->set_cookie("password",$user_info['password'],3600);
$this->input->set_cookie("user_id",$user_info['user_id'],3600);
//echo$this->input->cookie("password");//适用于控制器
//echo$this->input->cookie("username");//适用于控制器
//echo$_COOKIE['username'];//在模型类中可以通过这种方式获取cookie值
//echo$_COOKIE['password'];//在模型类中可以通过这种方式获取cookie值
//第三种设置cookie的方式:通过CI框架的cookie_helper.php函数库文件
//这种方式不是很灵验,建议大家采取第二种方式即可
//set_cookie("username",$user_info['username'],3600);
//echo get_cookie("username");
//session登陆时使用:将用户名和用户id存入session中
//$data['username']=$user_info['username'];
//$data['user_id']=$user_info['user_id'];
//$this->session->set_userdata($data);
//跳转到指定页面
//注意:site_url()与base_url()的区别,前者带index.php,后者不带index.php
header("location:".site_url("index/index"));
}
}else{
//跳转到登陆页面
header("location:".site_url("common/login"));
}
}
}
}
第二步:User_model.php
//cookie登陆:检测用户是否登陆,如果cookie值失效,则返回false,如果cookie值未失效,则根据cookie中的用户名和密码从数据库中获取用户信息,如果能获取到用户信息,则返回查询到的用户信息,如果没有查询到用户信息,则返回0
public function is_login(){
//获取cookie中的值
if(empty($_COOKIE['username'])|| empty($_COOKIE['password'])){
$user_info= false;
}else{
$user_info=$this->check_user_login($_COOKIE['username'],$_COOKIE['password']);
}
return$user_info;
}
//根据用户名及加密密码从数据库中获取用户信息,如果能获取到,则返回获取到的用户信息,否则返回false,注意:密码为加密密码
public function check_user_login($username,$password){
//这里大家要注意:$password为md5加密后的密码
//$this->db->query("select* from");
//快捷查询类的使用:能为我们提供快速获取数据的方法
//此数组为查询条件
//注意:关联数组
$arr=array(
'username'=>$username,//用户名
'password'=>$password,//加密密码
'status'=>1//账户为开启状态
);
//在database.php文件中已经设置了数据表的前缀,所以此时数据表无需带前缀
$query=$this->db->get_where("users",$arr);
//返回二维数组
//$data=$query->result_array();
//返回一维数组
$user_info=$query->row_array();
if(!empty($user_info)){
return$user_info;
}else{
return false;
}
}
第三步:其它控制器:
public function __construct(){
//调用父类的构造函数
parent::__construct();
$this->load->library('tp');//smarty模板解析类
$this->load->helper('url');//url函数库文件
$this->load->model("user_model");//User_model模型类实例化对象
$this->cur_user=$this->user_model->is_login();
if($this->cur_user=== false){
header("location:".site_url("common/login"));
}else{
//如果已经登陆,则重新设置cookie的有效期
$this->input->set_cookie("username",$this->cur_user['username'],3600);
$this->input->set_cookie("password",$this->cur_user['password'],3600);
$this->input->set_cookie("user_id",$this->cur_user['user_id'],3600);
}
$this->load->library('pagination');//分页类库
$this->load->model("role_model");//member_model模型类
$this->load->model("operation_model");//引用operation_model模型
$this->load->model("object_model");//引用object_model模型
$this->load->model("permission_model");//引用permission_model模型
}
php如何实现登录验证码
php实现登录验证码的方法:首先产生4到6位数的随机验证码;然后把产生的每个字符保存到session或数据库;接着将验证码发送到用户的手机;最后将和输入的验证码进行对比验证即可。
推荐:《PHP视频教程》
PHP实现简单的验证码功能机制
网站的安全性是开发者不可忽视的一个问题,目前使用最多的一种可以提高网站安全性的方法就是使用验证码功能机制,有的仅仅使用一个几位数字字母混乱的验证码,有的进行手机发送短信进行验证,有的使用邮箱发送邮件进行验证,但是这个验证码功能机制是如何实现的呢?下面就为大家详细解释验证码功能机制的实现思路以及简单的实现方法。
1、验证码功能机制实现思路
①常规的验证码实现:
a、产生一张png的图片
b、为图片设置背景色
c、设置字体颜色和样式
d、产生4位数的随机的验证码
e、把产生的每个字符调整旋转角度和位置画到png图片上
f、加入噪点和干扰线防止注册机器分析原图片来恶意注册
g、输出图片
h、释放图片所占内存
i、将验证码保存到session或是数据库
j、将和输入的验证码进行对比
②短信(邮箱)验证码机制:
a、产生4-6位数的随机的验证码
b、把产生的每个字符保存到session或是数据库
c、将验证码发送到用户的手机(邮箱)
d、用户在规定时间内进行输入
e、将验证码从session或是数据库中取出
f、将和输入的验证码进行对比验证
2、简单的实现验证码功能机制
①新建captcha.php,写入以下代码
<?php
/**
*=======================================
* Created by WeiBang Technology.
* User: Wei ZhiHua
* Date: 2016/10/12 0020
* Time:下午 4:14
* Power:实现验证码功能
*=======================================
*/
//开启session
session_start();
//创建一个大小为 100*30的验证码
$image= imagecreatetruecolor(100, 30);
$bgcolor= imagecolorallocate($image, 255, 255, 255);
imagefill($image, 0, 0,$bgcolor);
$captch_code='';
for($i= 0;$i< 4;$i++){
$fontsize= 6;
$fontcolor= imagecolorallocate($image, rand(0, 120), rand(0, 120), rand(0, 120));
$data='abcdefghijkmnpqrstuvwxy3456789';
$fontcontent= substr($data, rand(0, strlen($data)- 1), 1);
$captch_code.=$fontcontent;
$x=($i* 100/ 4)+ rand(5, 10);
$y= rand(5, 10);
imagestring($image,$fontsize,$x,$y,$fontcontent,$fontcolor);
}
//就生成的验证码保存到session
$_SESSION['authcode']=$captch_code;
//在图片上增加点干扰元素
for($i= 0;$i< 200;$i++){
$pointcolor= imagecolorallocate($image, rand(50, 200), rand(50, 200), rand(50, 200));
imagesetpixel($image, rand(1, 99), rand(1, 29),$pointcolor);
}
//在图片上增加线干扰元素
for($i= 0;$i< 3;$i++){
$linecolor= imagecolorallocate($image, rand(80, 220), rand(80, 220), rand(80, 220));
imageline($image, rand(1, 99), rand(1, 29), rand(1, 99), rand(1, 29),$linecolor);
}
//设置头
header('content-type:image/png');
imagepng($image);
imagedestroy($image);
?>②新建form.php,写入以下代码
文章到此结束,如果本次分享的php注册登录和开发者账号注册的问题解决了您的问题,那么我们由衷的感到高兴!