一些PHP写的小东西

林继 PHP 技术 6,343 共写了379个字 (2005-12-08 18:38:13) 没有评论 打印 扫描二维码 百度已收录

出处:blog.chinaunix.net

一些小东西有时候可能用得上!

1.得到客户端IP地址

  1. function getip(){ 
  2.         if (! empty($_SERVER["HTTP_X_FORWARDED_FOR"])){ //使用代理的情况 
  3.                 $tip = split(",", $_SERVER["HTTP_X_FORWARDED_FOR"]); 
  4.                 $cip = $tip[0]; 
  5.         } 
  6.         else 
  7.                 $cip = $_SERVER["REMOTE_ADDR"];[ 
  8.         return dechex(ip2long($cip)); 
  9. }

2.session控制的函数

  1. function session_begin(){ 
  2.         global $userid, $sid, $ip, $session, $db, $islogin; 
  3.         $ip = getip(); 
  4.         /* 短期而言,系统默认需要支持cookie. */ 
  5.         if (!isset($_COOKIE['userid'])) return false; 
  6.         else $userid = $_COOKIE['userid']; 
  7.         if (!isset($_COOKIE['sid'])) return false; 
  8.         else $sid = $_COOKIE['sid']; 
  9.         /* 以上返回false意味着用户没有登陆,页面将跳转至首页(登陆页). */ 
  10.  
  11.         $query = "SELECT * FROM user WHERE nickname = '$userid'"; 
  12.         $result = $db->sql_query($query); 
  13.         if ($row = $db->sql_fetchrow($result)){ 
  14.                 if ($row['sid'] != $sid) return false; /* cookie传送的sid和数据库保存的不吻合 */ 
  15.                 if ($row['lastloginip'] != $ip) return false; /* IP不吻合 */ 
  16.                 /* 是否要考虑 session的过期 问题呢? */ 
  17.         } 
  18.         else return false; /* 没有这个userid */ 
  19.         $session = $row; 
  20. //        $session[''] = $row['']; 
  21.         unset($row); 
  22.  
  23.         $islogin = true; 
  24.         return true; 
  25. } 
  26.  
  27. /* bool session_end(int $userid, string $sid) */ 
  28. function sesssion_end($userid, $sid){ 
  29.         return true; 
  30. }

3.做选美的投票程序

  1. <?php 
  2. header("Refresh:0;url=./");  //一秒刷新 
  3. ?> 
  4. <script>alert('<?php 
  5. require_once('mysql.php'); 
  6. require_once('functions.php'); 
  7. $db = new sql_db('localhost','root','','selectmm'); 
  8. $user_id = $_GET['user_id']; 
  9. $ip = getip(); 
  10. $deltime = time()-3600;  //减去一小时 
  11. $sql = "delete from vote where time <$deltime"; //删除掉过期的数据 
  12. $db->sql_query($sql); 
  13.  
  14. $sql = "select ip from vote where ip='$ip' and user_id='$user_id'"; //查看一小时内是否投过 
  15. $linkid=$db->sql_query($sql) or die(mysql_error()); 
  16. $count=$db->sql_affectedrows(); 
  17. if($count) 
  18. { 
  19.  echo "您已经投过票了!"; 
  20. } 
  21. else 
  22. { 
  23.  $sql = "update user set vote_count=vote_count+1 WHERE user_id='$user_id'"; 
  24.  $db->sql_query($sql); 
  25.  $count=$db->sql_affectedrows(); 
  26.  if($count) 
  27.  { 
  28.   echo "投票成功!"; 
  29.      $sql = "insert into vote (`ip`,`user_id`,`time`) values ('$ip','$user_id','".time()."')";//投票成功就插入一条记录。 
  30.      $db->sql_query($sql); 
  31.  
  32.  } 
  33.  else 
  34.  { 
  35.   echo "投票失败!"; 
  36.  } 
  37.  
  38. } 
  39. ?>'); 
  40. //history.back(); 
  41. </script>

4.smarty的搜索程序

  1. <?php 
  2. require_once('mysql.php'); 
  3. $db = new sql_db('localhost','root','','selectmm'); 
  4. require_once('functions.php'); 
  5. require_once('session.php'); 
  6. session_begin(); 
  7. require('./Libs/Smarty.class.php'); 
  8. $smarty = new Smarty; 
  9. $title = "首页"; 
  10. $smarty->assign("islogin",$islogin); 
  11. $smarty->assign("title",$title); 
  12. $age=$_GET['age']; 
  13. $arr=explode(',',$age); 
  14. $y=date('Y'); 
  15. $md=date('-m-d'); 
  16. $begin=($y-$arr[1]).$md; 
  17. $end=($y-$arr[0]).$md; 
  18. $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"; 
  19. $link=$db->sql_query($sql) or die(mysql_error()); 
  20. $row = $db->sql_fetchrowset($link); 
  21. $db->sql_freeresult(); 
  22. $smarty->assign("pic",$row); 
  23. $smarty->display('index.tpl.htm'); 
  24. ?>

5.注册程序

  1. <? 
  2. require('mysql.php'); 
  3. $str=new sql_db('localhost','root','','selectmm'); 
  4. $METHOD = $_POST; 
  5.     if (isset($METHOD['nickname']) && $METHOD['nickname'] != '') $nickname = $METHOD['nickname']; 
  6.    else { echo "<script>alert("用户昵称不能为空.")</script>"; echo "<script>location="register.php"</script>"; } 
  7.     if (isset($METHOD['password']) && strlen($METHOD['password'])>=6) $password = $METHOD['password']; 
  8.    else { echo "<script>alert("密码至少6")</script>"; echo "<a href='javascript:history.go(-2);'>"; } 
  9.     $password2 = $METHOD['password2']; 
  10.     if ($password != $password2) 
  11.    { echo "<script>alert("两次输入密码不一致")</script>"; echo "<script>location="register.php"</script>"; } 
  12.     if (isset($METHOD['name']) && $METHOD['name'] != '') $name = $METHOD['name']; 
  13.    else { echo "<script>alert("用户名不能为空.")</script>"; echo "<script>location="register.php"</script>"; } 
  14.      $birthday=$METHOD['Year'].$METHOD['Month'].$METHOD['Day']; 
  15.      $stature = $METHOD['stature']; 
  16.      $astrology = $METHOD['astrology']; 
  17.      $bloodtype = $METHOD['bloodtype']; 
  18.      $goodat = $METHOD['goodat']; 
  19.      $work = $METHOD['work']; 
  20.      $educate = $METHOD['educate']; 
  21.      $homeplace = $METHOD['homeplace']; 
  22.      $address = $METHOD['address']; 
  23.      $tel = $METHOD['tel']; 
  24.      $qq = $METHOD['qq']; 
  25.     if (isset($METHOD['email']) && $METHOD['email'] != '') $email = $METHOD['email']; 
  26.    else { echo "<script>alert("Email不能为空")</script>"; echo "<script>location="register.php"</script>"; } 
  27.     if (!eregi("^[0-9a-z.-_]+@[0-9a-z.]+.[a-z]$",$email)) { echo "<script>alert("电子邮件格式不合法")</script>"; echo "<script>location="register.php"</script>"; } 
  28.       $dian =$METHOD['dian']; 
  29.     if (isset($METHOD['myself']) && $METHOD['myself'] != '') $myself = $METHOD['myself']; 
  30.    else { echo "<script>alert(".....不能为空")</script>"; echo "<script>location="register.php"</script>"; } 
  31.     if(isset($METHOD['enounce'])&& $METHOD['enounce']!='')$enounce = $METHOD['enounce']; 
  32.     else { echo "<script>alert(".....不能为空")</script>"; echo "<script>location="register.php"</script>"; } 
  33.  
  34.  
  35.     $query = "SELECT * FROM user WHERE nickname ='$nickname' or email='$email'"; 
  36.     $result = $str->sql_query($query)or die(mysql_error()); ; 
  37.     if ($row = $str->sql_fetchrow($result)) 
  38.      { echo "<script>alert("对不起,该用户已经注册")</script>"; echo "<script>location="register.php"</script>"; } 
  39.     $password = md5($password); 
  40.     $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')"; 
  41. if($str->sql_query($query)) 
  42.       $str->sql_close(); 
  43.     echo "<script>alert("恭喜你,注册成功")</script>"; 
  44.  
  45.     echo "<script>location="login.php"</script>"; 
  46. ?>

6.提交参数 JS控制

  1. echo "<td><a href='delete.php?nickname=' onclick="return confirm('确定删除吗?rn此操作不可恢复')"><font color=red>删除该用户</font></a></td>";

7.在给同事做一个文本处理,两个文档一个有7万条记录,开始用嵌套循环,php死了,后面用数组解决了问题

  1. <?php 
  2. if(($fp=fopen("1.txt","a+"))===false) 
  3.    { 
  4.            die("打开文件失败"); 
  5.    } 
  6. $data1=file("old.txt") or die("打开文件失败"); 
  7. $data2=file("sports.txt") or die("打开文件失败"); 
  8. foreach($data1 as $data) 
  9. { 
  10. $x = split("[./]",$data); 
  11. $name = $x[count($x)-2]; 
  12. $a[$name]['md5']=$data; 
  13. } 
  14. foreach($data2 as $data) 
  15. { 
  16. $x = split("[,./]",$data); 
  17. $name = $x[count($x)-2]; 
  18. if(isset($a[$name])) 
  19. $a[$name]['name']=$x[0]; 
  20. } 
  21. foreach($a as $value) 
  22. { 
  23. $str=$value['md5'].','.$value['name']; 
  24. fwrite($fp,$str); 
  25. echo $value['md5'].','.$value['name']."<br>"; 
  26. fwrite($fp,$str); 
  27. } 
  28. ?>

8.验证码

  1. <?php 
  2. /* 
  3. * Filename:authimg.php 
  4. */ 
  5.  
  6. Header("Content-type:image/PNG"); 
  7. session_start(); 
  8. $auth_num = ""; 
  9. /*创建一个基于调色板的图像*/ 
  10. $im = imagecreate(63, 20); 
  11. /*初始化一个随机种子*/ 
  12. srand((double)microtime() * 1000000); 
  13. $auth_num_k = md5(rand(0, 9999)); 
  14. $auth_num = substr($auth_num_k, 17, 5); 
  15. /*赋值会话变量*/ 
  16. $_SESSION['authnum'] = $auth_num; 
  17. $black = ImageColorAllocate($im, 0, 0, 0); 
  18. $white = ImageColorAllocate($im, 255, 255, 255); 
  19. $gray = ImageColorAllocate($im, 200, 200, 200); 
  20. ImageFill($im, 63, 20, $black); 
  21. imagestring($im, 5, 10, 3, $auth_num,$gray); 
  22.  
  23. for ($i = 0;$i < 200;$i++){ 
  24.         $randcolor = ImageColorallocate($im, rand(0,255), rand(0,255), rand(0,255)); 
  25.         imagesetpixel($im, rand()%70, rand()%30, $randcolor); 
  26. } 
  27. ImagePNG($im); 
  28. ImageDestroy($im); 
  29. ?>

如果觉得我的文章对您有用,请随意赞赏。您的支持将鼓励我继续创作!

发表评论

电子邮件地址不会被公开。 必填项已用*标注

< >