一些PHP写的小东西_php技巧
程序员文章站
2022-05-31 17:55:12
...
一些小东西有时候可能用得上!
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.做选美的投票程序
header("Refresh:0;url=./"); //一秒刷新
?>
4.smarty的搜索程序
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 ""; echo ""; }
if (isset($METHOD['password']) && strlen($METHOD['password'])>=6) $password = $METHOD['password'];
else { echo ""; echo ""; }
$password2 = $METHOD['password2'];
if ($password != $password2)
{ echo ""; echo ""; }
if (isset($METHOD['name']) && $METHOD['name'] != '') $name = $METHOD['name'];
else { echo ""; echo ""; }
$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 ""; echo ""; }
if (!eregi("^[0-9a-z.-_]+@[0-9a-z.]+.[a-z]$",$email)) { echo ""; echo ""; }
$dian =$METHOD['dian'];
if (isset($METHOD['myself']) && $METHOD['myself'] != '') $myself = $METHOD['myself'];
else { echo ""; echo ""; }
if(isset($METHOD['enounce'])&& $METHOD['enounce']!='')$enounce = $METHOD['enounce'];
else { echo ""; echo ""; }
$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 ""; echo ""; }
$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 "";
echo "";
?>
6.提交参数 JS控制
echo "
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.做选美的投票程序
header("Refresh:0;url=./"); //一秒刷新
?>
4.smarty的搜索程序
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 ""; echo ""; }
if (isset($METHOD['password']) && strlen($METHOD['password'])>=6) $password = $METHOD['password'];
else { echo ""; echo ""; }
$password2 = $METHOD['password2'];
if ($password != $password2)
{ echo ""; echo ""; }
if (isset($METHOD['name']) && $METHOD['name'] != '') $name = $METHOD['name'];
else { echo ""; echo ""; }
$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 ""; echo ""; }
if (!eregi("^[0-9a-z.-_]+@[0-9a-z.]+.[a-z]$",$email)) { echo ""; echo ""; }
$dian =$METHOD['dian'];
if (isset($METHOD['myself']) && $METHOD['myself'] != '') $myself = $METHOD['myself'];
else { echo ""; echo ""; }
if(isset($METHOD['enounce'])&& $METHOD['enounce']!='')$enounce = $METHOD['enounce'];
else { echo ""; echo ""; }
$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 ""; echo ""; }
$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 "";
echo "";
?>
6.提交参数 JS控制
echo "
上一篇: PHP实现事件机制实例分析
下一篇: 常用内存数据库介绍