10 Useful PHP Code

Source: Internet
Author: User
Tags date format array empty get ip json php code query

get browser IP address

function getremoteipaddress () {

$ip = $_server[' remote_addr '];

return $IP;

}

Get IP If there is a proxy server

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 be pass from proxy

$ip = $_server[' http_x_forwarded_for '];

} else {

$ip = $_server[' remote_addr '];

}

return $IP;

}

Get 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;

}

Validate 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;

}

}

redirect

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

Send mail

$to = "Someone@oschina.net";

$subject = "Your subject here";

$body = "Body of your message is here for 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) {

$base = Base64_encode ($plainText);

$base 64url = STRTR ($base 64, ' +/= ', '-_, ');

return $base 64url;

}

function Base64url_decode ($plainText) {

$base 64url = STRTR ($plainText, '-_, ', ' +/= ');

$base = Base64_decode ($base 64url);

return $base 64;

}

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

Detecting user Browser types

$useragent = $_server [' http_user_agent '];

echo "Your User Agentis:". $useragent;

Display Web page 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 #<b>{$line _num}</b>:". Htmlspecialchars ($line). "<br>\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.