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 );
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 );
}
?>
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.