一些PHP写的小东西
程序员文章站
2023-11-26 16:18:34
一些小东西有时候可能用得上! 1.得到客户端ip地址 function getip(){ ...
一些小东西有时候可能用得上!
1.得到客户端ip地址
function getip(){
if (! empty($_server["http_x_forwarded_for"])){ //使用代理的情况
$tip = split(",", $_server["http_x_forwarded_for"]);
$cip = $tip[0];
}
else
$cip = $_server["remote_addr"];[
return dechex(ip2long($cip));
}
2.session控制的函数
function session_begin(){
global $userid, $sid, $ip, $session, $db, $islogin;
$ip = getip();
/* 短期而言,系统默认需要支持cookie. */
if (!isset($_cookie['userid'])) return false;
else $userid = $_cookie['userid'];
if (!isset($_cookie['sid'])) return false;
else $sid = $_cookie['sid'];
/* 以上返回false意味着用户没有登陆,页面将跳转至首页(登陆页). */
$query = "select * from user where nickname = '$userid'";
$result = $db->sql_query($query);
if ($row = $db->sql_fetchrow($result)){
if ($row['sid'] != $sid) return false; /* cookie传送的sid和数据库保存的不吻合 */
if ($row['lastloginip'] != $ip) return false; /* ip不吻合 */
/* 是否要考虑 session的过期 问题呢? */
}
else return false; /* 没有这个userid */
$session = $row;
// $session[''] = $row[''];
unset($row);
$islogin = true;
return true;
}
/* bool session_end(int $userid, string $sid) */
function sesssion_end($userid, $sid){
return true;
}
3.做选美的投票程序
<?php
header("refresh:0;url=./"); //一秒刷新
?>
<script>alert('<?php
require_once('mysql.php');
require_once('functions.php');
$db = new sql_db('localhost','root','','selectmm');
$user_id = $_get['user_id'];
$ip = getip();
$deltime = time()-3600; //减去一小时
$sql = "delete from vote where time <$deltime"; //删除掉过期的数据
$db->sql_query($sql);
$sql = "select ip from vote where ip='$ip' and user_id='$user_id'"; //查看一小时内是否投过
$linkid=$db->sql_query($sql) or die(mysql_error());
$count=$db->sql_affectedrows();
if($count)
{
echo "您已经投过票了!";
}
else
{
$sql = "update user set vote_count=vote_count+1 where user_id='$user_id'";
$db->sql_query($sql);
$count=$db->sql_affectedrows();
if($count)
{
echo "投票成功!";
$sql = "insert into vote (`ip`,`user_id`,`time`) values ('$ip','$user_id','".time()."')";//投票成功就插入一条记录。
$db->sql_query($sql);
}
else
{
echo "投票失败!";
}
}
?>');
//history.back();
</script>
4.smarty的搜索程序
<?php
require_once('mysql.php');
$db = new sql_db('localhost','root','','selectmm');
require_once('functions.php');
require_once('session.php');
session_begin();
require('./libs/smarty.class.php');
$smarty = new smarty;
$title = "首页";
$smarty->assign("islogin",$islogin);
$smarty->assign("title",$title);
$age=$_get['age'];
$arr=explode(',',$age);
$y=date('y');
$md=date('-m-d');
$begin=($y-$arr[1]).$md;
$end=($y-$arr[0]).$md;
$sql="select p.* from pic_info p,user u where p.user_id=u.user_id and u.birthday between '$begin' and '$end' group by u.user_id";
$link=$db->sql_query($sql) or die(mysql_error());
$row = $db->sql_fetchrowset($link);
$db->sql_freeresult();
$smarty->assign("pic",$row);
$smarty->display('index.tpl.htm');
?>
5.注册程序
<?
require('mysql.php');
$str=new sql_db('localhost','root','','selectmm');
$method = $_post;
if (isset($method['nickname']) && $method['nickname'] != '') $nickname = $method['nickname'];
else { echo "<script>alert("用户昵称不能为空.")</script>"; echo "<script>location="register.php"</script>"; }
if (isset($method['password']) && strlen($method['password'])>=6) $password = $method['password'];
else { echo "<script>alert("密码至少6位")</script>"; echo "<a href='javascript:history.go(-2);'>"; }
$password2 = $method['password2'];
if ($password != $password2)
{ echo "<script>alert("两次输入密码不一致")</script>"; echo "<script>location="register.php"</script>"; }
if (isset($method['name']) && $method['name'] != '') $name = $method['name'];
else { echo "<script>alert("用户名不能为空.")</script>"; echo "<script>location="register.php"</script>"; }
$birthday=$method['year'].$method['month'].$method['day'];
$stature = $method['stature'];
$astrology = $method['astrology'];
$bloodtype = $method['bloodtype'];
$goodat = $method['goodat'];
$work = $method['work'];
$educate = $method['educate'];
$homeplace = $method['homeplace'];
$address = $method['address'];
$tel = $method['tel'];
$qq = $method['qq'];
if (isset($method['email']) && $method['email'] != '') $email = $method['email'];
else { echo "<script>alert("email不能为空")</script>"; echo "<script>location="register.php"</script>"; }
if (!eregi("^[0-9a-z.-_]+@[0-9a-z.]+.[a-z]$",$email)) { echo "<script>alert("电子邮件格式不合法")</script>"; echo "<script>location="register.php"</script>"; }
$dian =$method['dian'];
if (isset($method['myself']) && $method['myself'] != '') $myself = $method['myself'];
else { echo "<script>alert("用.....不能为空")</script>"; echo "<script>location="register.php"</script>"; }
if(isset($method['enounce'])&& $method['enounce']!='')$enounce = $method['enounce'];
else { echo "<script>alert(".....不能为空")</script>"; echo "<script>location="register.php"</script>"; }
$query = "select * from user where nickname ='$nickname' or email='$email'";
$result = $str->sql_query($query)or die(mysql_error()); ;
if ($row = $str->sql_fetchrow($result))
{ echo "<script>alert("对不起,该用户已经注册")</script>"; echo "<script>location="register.php"</script>"; }
$password = md5($password);
$query = "insert into `user` (`nickname`,`password`,`name`,`birthday`,`astrology`,`bloodtype`,`stature`,`goodat`,`work`,`educate`,`homeplace`,`address`,`tel`,`email`,`qq`,`dian`,`myself`,`enounce`) values('$nickname','$password','$name','$birthday','$astrology','$bloodtype','$stature','$goodat','$work','$educate','$homeplace','$address','$tel','$email','$qq','$dian','$myself','$enounce')";
if($str->sql_query($query))
$str->sql_close();
echo "<script>alert("恭喜你,注册成功")</script>";
echo "<script>location="login.php"</script>";
?>
6.提交参数 js控制
echo "<td><a href='delete.php?nickname=' onclick="return confirm('确定删除吗?rn此操作不可恢复')"><font color=red>删除该用户</font></a></td>";
7.在给同事做一个文本处理,两个文档一个有7万条记录,开始用嵌套循环,php死了,后面用数组解决了问题
<?php
if(($fp=fopen("1.txt","a+"))===false)
{
die("打开文件失败");
}
$data1=file("old.txt") or die("打开文件失败");
$data2=file("sports.txt") or die("打开文件失败");
foreach($data1 as $data)
{
$x = split("[./]",$data);
$name = $x[count($x)-2];
$a[$name]['md5']=$data;
}
foreach($data2 as $data)
{
$x = split("[,./]",$data);
$name = $x[count($x)-2];
if(isset($a[$name]))
$a[$name]['name']=$x[0];
}
foreach($a as $value)
{
$str=$value['md5'].','.$value['name'];
fwrite($fp,$str);
echo $value['md5'].','.$value['name']."<br>";
fwrite($fp,$str);
}
?>
8.验证码
<?php
/*
* filename:authimg.php
*/
header("content-type:image/png");
session_start();
$auth_num = "";
/*创建一个基于调色板的图像*/
$im = imagecreate(63, 20);
/*初始化一个随机种子*/
srand((double)microtime() * 1000000);
$auth_num_k = md5(rand(0, 9999));
$auth_num = substr($auth_num_k, 17, 5);
/*赋值会话变量*/
$_session['authnum'] = $auth_num;
$black = imagecolorallocate($im, 0, 0, 0);
$white = imagecolorallocate($im, 255, 255, 255);
$gray = imagecolorallocate($im, 200, 200, 200);
imagefill($im, 63, 20, $black);
imagestring($im, 5, 10, 3, $auth_num,$gray);
for ($i = 0;$i < 200;$i++){
$randcolor = imagecolorallocate($im, rand(0,255), rand(0,255), rand(0,255));
imagesetpixel($im, rand()%70, rand()%30, $randcolor);
}
imagepng($im);
imagedestroy($im);
?>
1.得到客户端ip地址
function getip(){
if (! empty($_server["http_x_forwarded_for"])){ //使用代理的情况
$tip = split(",", $_server["http_x_forwarded_for"]);
$cip = $tip[0];
}
else
$cip = $_server["remote_addr"];[
return dechex(ip2long($cip));
}
2.session控制的函数
function session_begin(){
global $userid, $sid, $ip, $session, $db, $islogin;
$ip = getip();
/* 短期而言,系统默认需要支持cookie. */
if (!isset($_cookie['userid'])) return false;
else $userid = $_cookie['userid'];
if (!isset($_cookie['sid'])) return false;
else $sid = $_cookie['sid'];
/* 以上返回false意味着用户没有登陆,页面将跳转至首页(登陆页). */
$query = "select * from user where nickname = '$userid'";
$result = $db->sql_query($query);
if ($row = $db->sql_fetchrow($result)){
if ($row['sid'] != $sid) return false; /* cookie传送的sid和数据库保存的不吻合 */
if ($row['lastloginip'] != $ip) return false; /* ip不吻合 */
/* 是否要考虑 session的过期 问题呢? */
}
else return false; /* 没有这个userid */
$session = $row;
// $session[''] = $row[''];
unset($row);
$islogin = true;
return true;
}
/* bool session_end(int $userid, string $sid) */
function sesssion_end($userid, $sid){
return true;
}
3.做选美的投票程序
<?php
header("refresh:0;url=./"); //一秒刷新
?>
<script>alert('<?php
require_once('mysql.php');
require_once('functions.php');
$db = new sql_db('localhost','root','','selectmm');
$user_id = $_get['user_id'];
$ip = getip();
$deltime = time()-3600; //减去一小时
$sql = "delete from vote where time <$deltime"; //删除掉过期的数据
$db->sql_query($sql);
$sql = "select ip from vote where ip='$ip' and user_id='$user_id'"; //查看一小时内是否投过
$linkid=$db->sql_query($sql) or die(mysql_error());
$count=$db->sql_affectedrows();
if($count)
{
echo "您已经投过票了!";
}
else
{
$sql = "update user set vote_count=vote_count+1 where user_id='$user_id'";
$db->sql_query($sql);
$count=$db->sql_affectedrows();
if($count)
{
echo "投票成功!";
$sql = "insert into vote (`ip`,`user_id`,`time`) values ('$ip','$user_id','".time()."')";//投票成功就插入一条记录。
$db->sql_query($sql);
}
else
{
echo "投票失败!";
}
}
?>');
//history.back();
</script>
4.smarty的搜索程序
<?php
require_once('mysql.php');
$db = new sql_db('localhost','root','','selectmm');
require_once('functions.php');
require_once('session.php');
session_begin();
require('./libs/smarty.class.php');
$smarty = new smarty;
$title = "首页";
$smarty->assign("islogin",$islogin);
$smarty->assign("title",$title);
$age=$_get['age'];
$arr=explode(',',$age);
$y=date('y');
$md=date('-m-d');
$begin=($y-$arr[1]).$md;
$end=($y-$arr[0]).$md;
$sql="select p.* from pic_info p,user u where p.user_id=u.user_id and u.birthday between '$begin' and '$end' group by u.user_id";
$link=$db->sql_query($sql) or die(mysql_error());
$row = $db->sql_fetchrowset($link);
$db->sql_freeresult();
$smarty->assign("pic",$row);
$smarty->display('index.tpl.htm');
?>
5.注册程序
<?
require('mysql.php');
$str=new sql_db('localhost','root','','selectmm');
$method = $_post;
if (isset($method['nickname']) && $method['nickname'] != '') $nickname = $method['nickname'];
else { echo "<script>alert("用户昵称不能为空.")</script>"; echo "<script>location="register.php"</script>"; }
if (isset($method['password']) && strlen($method['password'])>=6) $password = $method['password'];
else { echo "<script>alert("密码至少6位")</script>"; echo "<a href='javascript:history.go(-2);'>"; }
$password2 = $method['password2'];
if ($password != $password2)
{ echo "<script>alert("两次输入密码不一致")</script>"; echo "<script>location="register.php"</script>"; }
if (isset($method['name']) && $method['name'] != '') $name = $method['name'];
else { echo "<script>alert("用户名不能为空.")</script>"; echo "<script>location="register.php"</script>"; }
$birthday=$method['year'].$method['month'].$method['day'];
$stature = $method['stature'];
$astrology = $method['astrology'];
$bloodtype = $method['bloodtype'];
$goodat = $method['goodat'];
$work = $method['work'];
$educate = $method['educate'];
$homeplace = $method['homeplace'];
$address = $method['address'];
$tel = $method['tel'];
$qq = $method['qq'];
if (isset($method['email']) && $method['email'] != '') $email = $method['email'];
else { echo "<script>alert("email不能为空")</script>"; echo "<script>location="register.php"</script>"; }
if (!eregi("^[0-9a-z.-_]+@[0-9a-z.]+.[a-z]$",$email)) { echo "<script>alert("电子邮件格式不合法")</script>"; echo "<script>location="register.php"</script>"; }
$dian =$method['dian'];
if (isset($method['myself']) && $method['myself'] != '') $myself = $method['myself'];
else { echo "<script>alert("用.....不能为空")</script>"; echo "<script>location="register.php"</script>"; }
if(isset($method['enounce'])&& $method['enounce']!='')$enounce = $method['enounce'];
else { echo "<script>alert(".....不能为空")</script>"; echo "<script>location="register.php"</script>"; }
$query = "select * from user where nickname ='$nickname' or email='$email'";
$result = $str->sql_query($query)or die(mysql_error()); ;
if ($row = $str->sql_fetchrow($result))
{ echo "<script>alert("对不起,该用户已经注册")</script>"; echo "<script>location="register.php"</script>"; }
$password = md5($password);
$query = "insert into `user` (`nickname`,`password`,`name`,`birthday`,`astrology`,`bloodtype`,`stature`,`goodat`,`work`,`educate`,`homeplace`,`address`,`tel`,`email`,`qq`,`dian`,`myself`,`enounce`) values('$nickname','$password','$name','$birthday','$astrology','$bloodtype','$stature','$goodat','$work','$educate','$homeplace','$address','$tel','$email','$qq','$dian','$myself','$enounce')";
if($str->sql_query($query))
$str->sql_close();
echo "<script>alert("恭喜你,注册成功")</script>";
echo "<script>location="login.php"</script>";
?>
6.提交参数 js控制
echo "<td><a href='delete.php?nickname=' onclick="return confirm('确定删除吗?rn此操作不可恢复')"><font color=red>删除该用户</font></a></td>";
7.在给同事做一个文本处理,两个文档一个有7万条记录,开始用嵌套循环,php死了,后面用数组解决了问题
<?php
if(($fp=fopen("1.txt","a+"))===false)
{
die("打开文件失败");
}
$data1=file("old.txt") or die("打开文件失败");
$data2=file("sports.txt") or die("打开文件失败");
foreach($data1 as $data)
{
$x = split("[./]",$data);
$name = $x[count($x)-2];
$a[$name]['md5']=$data;
}
foreach($data2 as $data)
{
$x = split("[,./]",$data);
$name = $x[count($x)-2];
if(isset($a[$name]))
$a[$name]['name']=$x[0];
}
foreach($a as $value)
{
$str=$value['md5'].','.$value['name'];
fwrite($fp,$str);
echo $value['md5'].','.$value['name']."<br>";
fwrite($fp,$str);
}
?>
8.验证码
<?php
/*
* filename:authimg.php
*/
header("content-type:image/png");
session_start();
$auth_num = "";
/*创建一个基于调色板的图像*/
$im = imagecreate(63, 20);
/*初始化一个随机种子*/
srand((double)microtime() * 1000000);
$auth_num_k = md5(rand(0, 9999));
$auth_num = substr($auth_num_k, 17, 5);
/*赋值会话变量*/
$_session['authnum'] = $auth_num;
$black = imagecolorallocate($im, 0, 0, 0);
$white = imagecolorallocate($im, 255, 255, 255);
$gray = imagecolorallocate($im, 200, 200, 200);
imagefill($im, 63, 20, $black);
imagestring($im, 5, 10, 3, $auth_num,$gray);
for ($i = 0;$i < 200;$i++){
$randcolor = imagecolorallocate($im, rand(0,255), rand(0,255), rand(0,255));
imagesetpixel($im, rand()%70, rand()%30, $randcolor);
}
imagepng($im);
imagedestroy($im);
?>
上一篇: 基于vue循环列表时点击跳转页面的方法
下一篇: PHP一些有意思的小区别