1500字范文,内容丰富有趣,写作好帮手!
1500字范文 > PHP 用户登录 验证码的生成 搜索代码

PHP 用户登录 验证码的生成 搜索代码

时间:2022-10-05 15:53:19

相关推荐

PHP 用户登录 验证码的生成 搜索代码

1.利用cookie技术实现用户的自动登录(注意密码需要md5加密)。

md5加密是使用最广泛的报文加密算法,它的作用就是把一个任意长度的字节串变换成一定长的大整数,并且它是一个不可逆的字符串变换算法,换句话说就是,即使你看到源程序和算法描述,也无法将一个MD5的值变换回原始的字符串.

一,用户登录的check

代码如下:

//检查用户是否登录

function checklogin(){

if(emptyempty($_SESSION['user_info'])){ //检查一下session是不是为空

if(emptyempty($_COOKIE['username']) || emptyempty($_COOKIE['password'])){ //如果session为空,并且用户没有选择记录登录状

header(”location:login.php?req_url=”.$_SERVER['REQUEST_URI']); //转到登录页面,记录请求的url,登录后跳转过去,用户体验好。

}else{ //用户选择了记住登录状态

$user = getUserInfo($_COOKIE['username'],$_COOKIE['password']); //去取用户的个人资料

if(emptyempty($user)){ //用户名密码不对没到取到信息,转到登录页面

header(”location:login.php?req_url=”.$_SERVER['REQUEST_URI']);

}else{

$_SESSION['user_info'] = $user; //用户名和密码对了,把用户的个人资料放到session里面

}

}

}

}

在访问后台的每个页面时,都要先进行上面的检查

二,用户提交登录信息

当用户填写用户名和密码后就提交到这儿来,

代码如下:

$username = trim($_POST['username']);

$password = md5(trim($_POST['password']));

$validatecode = $_POST['validateCode'];

$ref_url = $_GET['req_url'];

$remember = $_POST['remember'];

$err_msg = ”;

if($validatecode!=$_SESSION['checksum']){

$err_msg = “验证码不正确”;

}elseif($username==” || $password==”){

$err_msg = “用户名和密码都不能为空”;

}else{

$row = getUserInfo($username,$password);

if(emptyempty($row)){

$err_msg = “用户名和密码都不正确”;

}else{

$_SESSION['user_info'] = $row;

if(!emptyempty($remember)){ //如果用户选择了,记录登录状态就把用户名和加了密的密码放到cookie里面

setcookie(”username”, $username, time()+3600*24*365);

setcookie(”password”, $password, time()+3600*24*365);

}

if(strpos($ref_url,”login.php”) === false){

header(”location:”.$ref_url);

}else{

header(”location:main_user.php”);

}

}

}

$username = trim($_POST['username']);

$password = md5(trim($_POST['password']));

$validatecode = $_POST['validateCode'];

$ref_url = $_GET['req_url'];

$remember = $_POST['remember'];

$err_msg = ”;

if($validatecode!=$_SESSION['checksum']){

$err_msg = “验证码不正确”;

}elseif($username==” || $password==”){

$err_msg = “用户名和密码都不能为空”;

}else{

$row = getUserInfo($username,$password);

if(empty($row)){

$err_msg = “用户名和密码都不正确”;

}else{

$_SESSION['user_info'] = $row;

if(!empty($remember)){ //如果用户选择了,记录登录状态就把用户名和加了密的密码放到cookie里面

setcookie(”username”, $username, time()+3600*24*365);

setcookie(”password”, $password, time()+3600*24*365);

}

if(strpos($ref_url,”login.php”) === false){

header(”location:”.$ref_url);

}else{

header(”location:main_user.php”);

}

}

}

2.验证码的生成及验证

验证码的生成:<?php

$im = imagecreatetruecolor(80,23);//创建画布

$bgcolor = imagecolorallocate($im,220,230,230);//调制背景色

$bordercolor = imagecolorallocate($im,0,0,255);//调制边框颜色

$tcolor = imagecolorallocate($im,255,0,0);

$green = imagecolorallocate($im,0,255,0);

imagefill($im,10,10,$bgcolor);//填充背景色

imagerectangle($im,1,1,79,22,$bordercolor);//绘制边框

for($i = 0;$i < 4; $i++){

$num_case = rand(0,2);//产生随机数0-2,根据数值的不同决定产生的是数字|小写|大写

switch($num_case){

case 0:$num = rand(48,57);break;//数字

case 1:$num = rand(65,90);break;//大写

default:$num = rand(97,122);//小写

}

$text[$i] = sprintf("%c",$num);//将随机产生的ASCII码转换为相应的字符

imagettftext($im,rand(13,22),rand(0,30),15*$i+7,20,$tcolor,"simhei.ttf",$text[$i]);//显示字符

}

for($i=0;$i<100;$i++){

imagesetpixel($im,rand(1,79),rand(1,22),$green);

}

session_start();

$_SESSION["ckcode"]=implode($text);

header("Content-type:image/png");//设置输出类型

imagepng($im);//输出图像

imagedestroy($im);

function rand_check()

{

if($_POST["reg_rand"] == $_SESSION["login_check_num"]){

return true;

}

else{

exit("验证码输入错误");

}

}

?>

验证码的验证:<?php

class CheckCode {

private $width;

private $height;

private $im;

private $text;

function __construct($width=100,$height=20){

$this->width = $width;

$this->height = $height;

}

function CreateCode(){

$this->ImageIni();//初始化图像

$this->CreateText();//生成验证码

$this->setDisturbColor();//设置干扰点

$this->outputImage();//向浏览器输出图片

}

//定义初始图像函数

private function ImageIni(){

$this->im = imagecreatetruecolor($this->width,$this->height);

$bgcolor = imagecolorallocate($this->im,rand(0,255),rand(0,255),rand(0,255));

$bordercolor = imagecolorallocate($this->im,0,0,0);

imagefill($this->im,$this->width-1,$this->height-1,$bgcolor);

imagerectangle($this->im,1,1,$this->width-1,$this->height-1,$bordercolor);

}

//定义产生随机字符函数

private function CreateText(){

$tcolor = imagecolorallocate($this->im,125,0,255);

for($i = 0;$i < 4; $i++){

$num_case = rand(0,2);//产生随机数0-2,根据数值的不同决定产生的是数字|小写|大写

switch($num_case){

case 0:$num = rand(48,57);break;//数字

case 1:$num = rand(65,90);break;//大写

default:$num = rand(97,122);//小写

}

$this->text[$i] = sprintf("%c",$num);//将随机产生的ASCII码转换为相应的字符

imagettftext($this->im,$this->height-5,rand(0,45),(($this->width-10)/4)*$i+5,$this->height-3,$tcolor,"simhei.ttf",$this->text[$i]);//显示字符

}

}

//创建放置干扰点函数

private function setDisturbColor(){

$green = imagecolorallocate($this->im,0,255,0);

for($i=0;$i<100;$i++){

imagesetpixel($this->im,rand(1,$this->width-1),rand(1,$this->height-1),$green);

}

}

//创建图像输出函数

private function outputImage(){

if (imagetypes() & IMG_GIF){

header("Content-type:image/gif");//设置输出类型

imagegif($this->im);//输出图像

}

elseif(imagetypes() & IMG_PNG){

header("Content-type:image/png");//设置输出类型

imagepng($this->im);//输出图像

}

elseif(imagetypes() & IMG_JPG){

header("Content-type:image/jpeg");//设置输出类型

imagejpeg($this->im);//输出图像

}

elseif(imagetypes() & IMG_WBMP){

header("Content-type:image/wbmp");//设置输出类型

imagewbmp($this->im);//输出图像

}

else {

die("php不支持您的图像创建");

}

}

function getMessage(){

return implode($this->text);

}

}

//析构函数销毁图像,释放内存

function __destruct(){

imagedestroy($this->im);

}

?>

<?php

session_start();

if($_POST[check]){

if($_POST[check]==$_SESSION[check_pic]){

echo "输入验证码正确";

}else{

echo "验证码有误"; }

?>

3.搜索建议的实现

elseif($_REQUEST['act'] == 'search'){

//进行搜索的判断,先获得用户输入的内容

$brand_name = $_POST['brand_name'];

//然后根据用户提交的内容进行查询

$sql = "select * from brand where brand_name = '$brand_name'";

include 'includes/db.class.php';

$db = new db('localhost','root','123','ecshop_test');

$result = mysql_query($sql);

$return = array();

while($all_row = mysql_fetch_array($result)){

$return[] = $all_row;

};

include 'templates/brand_list.php';

brand_list.php中改写的内容:

<form action="brand.php?act=search" name="searchForm" method="post">

<img src="images/icon_search.gif" width="26" height="22" border="0" alt="SEARCH" />

<input type="text" name="brand_name" size="15" value="<?php echo isset($brand_name)?$brand_name:'';?>"/>

<input type="submit" value=" 搜索 " class="button" />

</form>

如果商品不存在会提示:您所查找的商品不存在!

<?php endforeach;?>

<?php else:?>

<tr><td align="center" nowrap="true" colspan="6"><?php echo '您查找的商品不存在';?></td></tr>

<?php endif;?>

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。