10 Useful PHP code

Source: Internet
Author: User
Get the browser ip address functiongetRemoteIPAddress () {$ ip =$ _ SERVER [& amp; #39; REMOTE_ADDR & amp; #39;]; return $ ip ;} obtain IPfunctiongetRealIPAddress () {I

Get browser IP address

Function getRemoteIPAddress (){

$ Ip = $ _ SERVER ['remote _ ADDR '];

Return $ ip;

}

 

Obtain the IP address if a proxy server exists.

Function getRealIPAddress (){

If (! Empty ($ _ SERVER ['http _ CLIENT_IP ']) {// check ip from share internet

$ Ip = $ _ SERVER ['http _ CLIENT_IP '];

} Elseif (! Empty ($ _ SERVER ['http _ X_FORWARDED_FOR ']) {// to check ip is pass from proxy

$ Ip = $ _ SERVER ['http _ X_FORWARDED_FOR '];

} Else {

$ Ip = $ _ SERVER ['remote _ ADDR '];

}

Return $ ip;

}

 

Obtain the MySQL timestamp

$ Query = "select UNIX_TIMESTAMP (date_field) as mydate from mytable where 1 = 1 ";

$ Records = mysql_query ($ query) or die (mysql_error ());

While ($ row = mysql_fetch_array ($ records )){

Echo $ row;

}

 

Validation date format: YYYY-MM-DD

Function checkDateFormat ($ date ){

// Match the format of the date

If (preg_match ("/^ ([0-9] {4})-([0-9] {2})-([0-9] {2 }) $/", $ date, $ parts )){

// Check whether the date is valid of not

If (checkdate ($ parts [2], $ parts [3], $ parts [1]) {

Return true;

} Else {

Return false;

}

} Else {

Return false;

}

}

 

Redirection

Header ('Location: http://www.php100.com ');

 

Send email

$ To = "someone@oschina.net ";

$ Subject = "Your Subject here ";

$ Body = "Body of your message here you can use HTML too. e.g.
Bold";

$ Headers = "From: You \ r \ n ";

$ Headers. = "Reply-To: info@yoursite.com \ r \ n ";

$ Headers. = "Return-Path: info@yoursite.com \ r \ n ";

$ Headers. = "X-Mailer: PHP \ n ";

$ Headers. = 'Mime-Version: 1.0 '. "\ n ";

$ Headers. = 'content-type: text/html; charset = ISO-8859-1 '. "\ r \ n ";

Mail ($ to, $ subject, $ body, $ headers );

 

BASE64 encoding and decoding

Function base64url_encode ($ plainText ){

$ Base64 = base64_encode ($ plainText );

$ Base64url = strtr ($ base64, '+/= ','-_,');

Return $ base64url;

}

Function base64url_decode ($ plainText ){

$ Base64url = strtr ($ plainText, '-_,', '+/= ');

$ Base64 = base64_decode ($ base64url );

Return $ base64;

}

 

JSON processing

$ Json_data = array ('id' => 1, 'name' => "John", 'Country' => 'Canada ', "work" => array ("Google", "Oracle "));

Echo json_encode ($ json_data );

$ Json_string = '{"id": 1, "name": "John", "country": "Canada", "work": ["Google ", "Oracle"]} ';

$ Obj = json_decode ($ json_string );

// Print the parsed data

Echo $ obj-> name; // displays John

Echo $ obj-> work [0]; // displays Google

 

Detect user browser types

$ Useragent = $ _ SERVER ['http _ USER_AGENT '];

Echo"Your User Agent is: ". $ Useragent;

 

Show webpage source code

$ Lines = file ('http: // www.php100.com/index.php ');

Foreach ($ lines as $ line_num => $ line ){

// Loop thru each line and prepend line numbers

Echo "Line #{$ Line_num}: ". Htmlspecialchars ($ line )."
\ N ";

}

 

Adjust server time

$ Now = date ('Y-m-d-G ');

$ Now = strftime ("% Y-% m-% d-% H", strtotime ("$ now-8 hours "));

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.