Some PHP writing little things _php tips

Source: Internet
Author: User
Some little things can be used sometimes!


1. Get Client IP Address


function GetIP () {
if (! empty ($_server["http_x_forwarded_for"])) {//Use agent case
$tip = Split (",", $_server["http_x_forwarded_for"]);
$CIP = $tip [0];
}
Else
$cip = $_server["REMOTE_ADDR"]; [
Return Dechex (Ip2long ($CIP));
}


2.session-controlled 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 '];
/* Above return false means that the user does not login, the page will jump to the first page (landing pages). */

$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 delivery SID and database save not match * *
if ($row [' Lastloginip ']!= $ip) return false; /* IP does not match * *
/* Do you want to consider expiration of session? */
}
else return false; * * without this 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. Voting procedures for beauty pageants


<?php
Header ("refresh:0;url=./"); One second refresh
?>
<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; Minus one hour.
$sql = "Delete from vote where time < $deltime"; Delete expired data
$db->sql_query ($sql);

$sql = "Select IP from vote where ip= ' $ip ' and user_id= ' $user _id '"; See if you've voted in an hour.
$linkid = $db->sql_query ($sql) or Die (Mysql_error ());
$count = $db->sql_affectedrows ();
if ($count)
{
echo "You've already voted!" ";
}
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 "Vote for Success! ";
$sql = "INSERT INTO vote (' IP ', ' user_id ', ' time ') VALUES (' $ip ', ' $user _id ', '", ". Time ()." ') ";/ /vote successfully insert a record.
$db->sql_query ($sql);

}
Else
{
echo "Vote failed! ";
}

}
?> ');
History.back ();
</script>


4.smarty Search Program


<?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 = "Home page";
$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.us ER_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. Registration procedure


?
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 (" user nickname cannot be empty. ") </script> "; echo "<script>location=" register.php "</script>"; }
if (Isset ($METHOD [' Password ']) && strlen ($METHOD [' Password ']) >=6) $password = $METHOD [' Password '];
else {echo "<script>alert (" password at least 6 digits ") </script>" echo "<a href= ' Javascript:history.go (-2); ' > "; }
$password 2 = $METHOD [' Password2 '];
if ($password!= $password 2)
{echo "<script>alert (inconsistent two input passwords)" </script> "<script>location= echo" "register.php" </script > "; }
if (Isset ($METHOD [' name ']) && $METHOD [' name ']!= ') $name = $METHOD [' name '];
else {echo "<script>alert (" User name cannot be empty. ") </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 cannot be empty ") </script>" echo "<script>location=" register.php "</ Script> "; }
if (!eregi ("^[0-9a-z.-_]+@[0-9a-z.] +. [a-z]$ ", $email)) {echo" <script>alert ("e-mail format is not valid") </script> "; echo" <script>location= " register.php "</script>"; }
$dian = $METHOD [' Dian '];
if (Isset ($METHOD [' myself ']) && $METHOD [' Myself ']!= ') $myself = $METHOD [' Myself '];
else {echo "<script>alert" (with ...). Cannot be empty ") </script>"; echo "<script>location=" register.php "</script>"; }
if (Isset ($METHOD [' enounce ']) && $METHOD [' enounce ']!= ') $enounce = $METHOD [' enounce '];
else {echo "<script>alert" ... Cannot be empty ") </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 (" Sorry, the user has been registered ") </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 (" Congratulations, Registered success ") </script>";

echo "<script>location=" login.php "</script>";
?>


6. Submit Parameter JS Control


echo "<td><a href= ' delete.php?nickname= ' onclick=" return confirm (' OK delete? RN This operation cannot be recovered ') "><font color=red > Delete the user </font></a></td> ";


7. To do a text processing for colleagues, two documents one has 70,000 records, start with nested loops, PHP dead, followed by an array to solve the problem


<?php
if (($fp =fopen ("1.txt", "A +")) ===false)
{
Die ("Open file Failed");
}
$data 1=file ("Old.txt") or Die ("Open file Failed");
$data 2=file ("Sports.txt") or Die ("Open file Failed");
foreach ($data 1 as $data)
{
$x = Split ("[./]", $data);
$name = $x [Count ($x)-2];
$a [$name] [' MD5 ']= $data;
}
foreach ($data 2 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. Verification Code


<?php
/*
* Filename:authimg.php
*/

Header ("Content-type:image/png");
Session_Start ();
$auth _num = "";
/* Create a palette-based image * *
$im = imagecreate (63, 20);
/* Initialize a random seed * *
Srand (Double) microtime () * 1000000);
$auth _num_k = MD5 (rand (0, 9999));
$auth _num = substr ($auth _num_k, 17, 5);
/* Assignment Session variable * *
$_session[' authnum ' = $auth _num;
$black = imagecolorallocate ($im, 0, 0, 0);
$white = Imagecolorallocate ($im, 255, 255, 255);
$gray = Imagecolorallocate ($im, 200, 200, 200);
Imagefill ($im, $black);
Imagestring ($im, 5, 3, $auth _num, $gray);

for ($i = 0; $i < $i + +) {
$randcolor = Imagecolorallocate ($im, Rand (0,255), Rand (0,255), Rand (0,255));
Imagesetpixel ($im, Rand ()%70, Rand ()%30, $randcolor);
}
Imagepng ($im);
Imagedestroy ($im);
?>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.