一些小東西有時候可能用得上!
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);
?>