Something written in PHP

Source: Internet
Author: User
Tags random seed
Some little things written in PHP may sometimes be used!


1. obtain the client IP address.


Function getip (){
If (! Empty ($ _ SERVER ["HTTP_X_FORWARDED_FOR"]) {// proxy usage
$ 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 must support cookies by default .*/
If (! Isset ($ _ COOKIE ['userid']) return false;
Else $ userid = $ _ COOKIE ['userid'];
If (! Isset ($ _ COOKIE ['Sid ']) return false;
Else $ sid =$ _ COOKIE ['Sid '];
/* If the returned value is false, the user does not log on. The page will jump to the homepage (login page ).*/

$ Query = "SELECT * FROM user WHERE nickname = '$ userid '";
$ Result = $ db-> SQL _query ($ query );
If ($ row = $ db-> SQL _fetchrow ($ result )){
If ($ row ['Sid ']! = $ Sid) return false;/* The sid transmitted by the cookie does not match that saved in the database */
If ($ row ['lastloginip']! = $ Ip) return false;/* IP does not match */
/* Do you need to consider the session expiration issue? */
}
Else return false;/* This userid does not exist */
$ 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. make a voting program for the beauty contest


Header ("Refresh: 0; url =./"); // Refresh in one second
?>
Script alert (' 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 '"; // check whether the ip address has been cast in one hour
$ Linkid = $ db-> SQL _query ($ SQL) or die (mysql_error ());
$ Count = $ db-> SQL _affectedrows ();
If ($ count)
{
Echo "you have 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 successful! ";
$ SQL = "insert into vote ('IP', 'User _ id', 'time') values ('$ IP',' $ user_id ','". time (). "')"; // Insert a record when the vote is successful.
$ Db-> SQL _query ($ SQL );

}
Else
{
Echo "failed to vote! ";
}

}
?> ');
// History. back ();
Script


4. smarty search program


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 = "homepage ";
$ 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. Register the program


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 blank. ") 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 characters") script "; echo "";}
$ Password2 = $ METHOD ['password2 '];
If ($ password! = $ Password2)
{Echo "script" alert ("inconsistent passwords entered twice") script "; echo" script "location =" register. php "script";}
If (isset ($ METHOD ['name']) & $ METHOD ['name']! = '') $ Name = $ METHOD ['name'];
Else {echo "script" alert ("user name cannot be blank.") 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 ['ucate'];
$ 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 blank") script "; echo" script "location =" register. php "script";}
If (! Eregi ("^ [0-9a-z. -_] + @ [0-9a-z.] +. [a-z] $ ", $ email) {echo" script "alert (" invalid email format ") script"; echo "script location =" register. php "script";}
$ Dian = $ METHOD ['Dian'];
If (isset ($ METHOD ['Myself ']) & $ METHOD ['Myself']! = '') $ Myself = $ METHOD ['Myself '];
Else {echo "script" alert ("..... cannot be blank ") script"; echo "script location =" register. php "script";}
If (isset ($ METHOD ['enounce ']) & $ METHOD ['enounce']! = '') $ Enounce = $ METHOD ['enounce '];
Else {echo "script" alert ("..... cannot be blank ") 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, this user has already registered") script "; echo" script "location =" register. php "script";}
$ Password = md5 ($ password );
$ Query = "insert into 'user' ('nickname', 'password', 'name', 'birthday', 'Astrology ', 'bloodtype', 'stature ', 'goodat ', 'work', 'ucate', 'homechang', 'address', 'tel', 'Email', 'QQ', 'Dian', 'myself-service ', '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, registration successful") script ";

Echo "script location =" login. php "script";
?>


6. submit parameter JS control


Echo"Delete this user";


7. I am doing a text processing for my colleagues. one of the two documents has 70 thousand Records and started to use nested loops. php is dead, and the problem is solved with arrays later.


If ($ fp = fopen ("1.txt"," a + ") = false)
{
Die ("failed to open the file ");
}
$ Data1 = file ("old.txt") or die ("file opening failed ");
$ Data2 = file ("sports.txt") or die ("failed to open the file ");
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. verification code


/*
* 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 );
/* Assign a 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, 63, 20, $ black );
Imagestring ($ im, 5, 10, 3, $ auth_num, $ gray );

For ($ I = 0; I 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 );
?>

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.