Home Backend Development PHP Tutorial Some little things written in PHP_PHP tutorial

Some little things written in PHP_PHP tutorial

Jul 21, 2016 pm 03:59 PM
php address client

Some little things may come in handy sometimes!


1. Get the client IP address


function getip(){
if (! empty($_SERVER ["HTTP_X_FORWARDED_FOR"])){ //Use proxy
$tip = split(",", $_SERVER["HTTP_X_FORWARDED_FOR"]);
$cip = $tip[0];
}
else
$cip = $_SERVER["REMOTE_ADDR"];[
return dechex(ip2long($cip));
}


2.ses sion control Functions


function session_begin(){
global $userid, $sid, $ip, $session, $db, $islogin;
$ip = getip();
/* In the short term, the system needs to support cookies by default. */
if (!isset($_COOKIE['userid'])) return false;
else $userid = $_COOKIE['userid'];
if (!isset($_COOKIE['sid'])) return false;
else $sid = $_COOKIE['sid'];
/* Returning false above means the user is not logged in, the page Will jump to the homepage (login page). */

$query = "SELECT * FROM user WHERE nickname = '$userid'";
$result = $db->sql_query($query ; The preserved non -kissing*/
if ($ row ['latloginip']! = $ IP) Return false; 🎜>} Else Return false;/*Without this userid*/
$ session = $ row;
// $ session [''] = $ row ['']; $row);

$islogin = true;
return true;
}

/* bool session_end(int $userid, string $sid) */
function sesssion_end($userid, $sid){
return true;
}


3. The voting procedure for the beauty pageant


header("Refresh:0;url=./"); //Refresh in one second
?>
<script>alert('<?php <br>require_once('mysql.php' ); <br>require_once('functions.php'); <BR>$db = new sql_db('localhost','root','','selectmm'); <BR>$user_id = $_GET['user_id ']; <BR>$ip = getip(); <BR>$deltime = time()-3600; //Subtract one hour <BR>$sql = "delete from vote where time <$deltime"; / /Delete expired data <BR>$db->sql_query($sql); <br><br>$sql = "select ip from vote where ip='$ip' and user_id='$user_id'"; //Check whether it has been submitted within one hour <br>$linkid=$db->sql_query($sql) or die(mysql_error()); <br>$count=$db->sql_affectedrows(); <br>if($count) <br>{ <br>echo "You have already voted!"; <br>} <br>else <br>{ <br>$sql = "update user set vote_count=vote_count+1 WHERE user_id='$user_id'"; <br>$db->sql_query($sql ); <br>$count=$db->sql_affectedrows(); <br>if($count) <br>{ <br> echo "The vote was successful! "; <br> $sql = "insert into vote (`ip`,`user_id`,`time`) values ​​('$ip','$user_id','".time()."')";/ /Insert a record if the vote is successful.<br> $db->sql_query($sql); <br><br>} <br>else <br>{ <br> echo "Vote failed!";  <br>}  <br><br>}  <br>?>');  <br>//history.back();  <br></script> 


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 "<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 ""; } 
   $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 "删除该用户"; 


7.在给同事做一个文本处理,两个文档一个有7万条记录,开始用嵌套循环,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']."
"; 
fwrite($fp,$str); 

?> 


8.验证码 


/* 
* 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); 
?>

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/317307.htmlTechArticleSome little things may be useful sometimes! 1. Get the client IP address functiongetip(){ if(! empty($_SERVER["HTTP_X_FORWARDED_FOR"])){//Using a proxy $tip=split(",",$_SERVER["HTT...
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

CakePHP Working with Database CakePHP Working with Database Sep 10, 2024 pm 05:25 PM

Working with database in CakePHP is very easy. We will understand the CRUD (Create, Read, Update, Delete) operations in this chapter.

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

To work on file upload we are going to use the form helper. Here, is an example for file upload.

CakePHP Routing CakePHP Routing Sep 10, 2024 pm 05:25 PM

In this chapter, we are going to learn the following topics related to routing ?

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

CakePHP Creating Validators CakePHP Creating Validators Sep 10, 2024 pm 05:26 PM

Validator can be created by adding the following two lines in the controller.

CakePHP Logging CakePHP Logging Sep 10, 2024 pm 05:26 PM

Logging in CakePHP is a very easy task. You just have to use one function. You can log errors, exceptions, user activities, action taken by users, for any background process like cronjob. Logging data in CakePHP is easy. The log() function is provide

See all articles