這一天終於來了,今後你不只僅「不該該」使用mysql_函數。PHP 7 已經把它們從核心中所有移除了,也就是說你須要遷移到好得多的mysqli_函數,或者更靈活的 PDO 實現。php
這一條可能易於理解,可是會變得愈來愈重要,由於 PHP 7 的速度提高可能會隱藏你的一些問題。不要僅僅知足於你的站點速度,由於遷移到 PHP 7 才讓它變快。css
爲了理解速度有多重要,以及如何把事情作得更好,請看一看咱們的文章速度優化入門指南。html
做爲一名開發者,你應該老是確保按需加載腳本,儘量鏈接它們,編寫高效的數據庫查詢,儘量使用緩存,以及其它。python
你能夠看一看,當一個文件以 PHP 代碼結尾時,WordPress 多數核心代碼都把末尾的 PHP 標籤去掉了。實際上,Zend 框架特別禁止了它。PHP 並不須要文件末尾的閉合標籤,而且咱們能夠經過去掉它來保證不會在後面添加任何的空白字符。mysql
我我的不喜歡引用傳遞。我知道有時候它很實用,可是其它狀況下它使代碼變得難懂,而且更難預測結果。laravel
聽說一些人認爲它使代碼運行更快,可是根據一些 PHP 高級程序員所說,這並不正確。git
說明引用爲何很差的一個例子是,PHP 內建了shuffle()和sort()。它們修改原始數組,而不是返回處理後的數組,這很不合邏輯。程序員
在循環中執行查詢很是浪費。它給你的系統施加沒必要要的壓力,而且可能可以在循環外部更快得到相同結果。當我遇到須要這樣的狀況時,我一般會使用兩個分離的查詢來解決問題,我會使用它們來構建數據數組。以後我會遍歷數組,並不須要在這個過程當中執行查詢。github
因爲 WordPress 適用於這裏,它可能有一些例外。雖然get_post_meta() 會從數據庫獲取大量數據,若是你正在遍歷某個特殊博文的元數據你能夠在循環中使用它。這是由於當你第一次調用它的時候,WordPress實際上會獲取全部元數據並緩存它們。後續的調用使用這些緩存數據,沒有數據庫的調用。web
弄懂這些的最佳方式是閱讀函數文檔,以及使用相似 Query Monitor 的工具。
固然,這個更像 MySQL 的問題,可是咱們習慣在 PHP 中編寫 SQL 代碼,因此都差很少。不管如何,若是能夠避免的話,不要在 SQL 查詢裏使用通配符,尤爲是數據庫有不少列的時候。
你應該明確指定須要哪些行,而且僅僅獲取它們。這有助於減小所用資源,保護數據,以及讓事情變得儘量清晰。
對於 SQL,你須要瞭解全部可用的函數,而且儘量測試其速度。在計算均值、求和或計算相似數值時,要使用 SQL 函數而不是 PHP 函數。若是你不肯定某個查詢的速度,測試它而且嘗試一些其它的編譯 -- 以後使用最好的那個。
信任用戶輸入是不明智的。始終校驗、過濾、轉義、檢查並留好退路。用戶數據存在三個問題:咱們開發者並無考慮每種可能性,它一般不正確,以及它多是蓄意破壞。
通過周密考慮的系統能夠防禦這些威脅。要確保使用相似filter_var()的內建函數檢查適當的值,以及在處理數據庫時轉義(或預編譯)。
WordPress 擁有一些函數來解決問題。詳見文章校驗、轉義和過濾用戶數據。
你的目標應該是編寫優雅的代碼,來更清晰地表達你的意圖。你可能可以經過將任何東西縮短爲一個單詞的變量,使用多層的三元邏輯,以及其它手段,從每一個頁面中優化 0.01 秒。但這隻會給你和你周圍的人產生大麻煩。
合理命名變量,爲代碼編寫文檔,優先選擇清晰而不是簡潔。甚至還能夠更好,使用標準的面向對象代碼,它自己或多或少就是文檔,不須要一大堆內聯數值。
PHP 到如今爲止有很長時間了,網站被造出來的時間更長。極可能不管你須要造出什麼,一些人以前早就造出來了。不要懼怕向他人尋求支持,Github是你的好朋友,Composer也是,Packagist也是。
從日誌工具到調色工具,從性能分析器到單元測試框架,從 Mailchimp API 到 Twitter Bootstrap,每一個東西均可以經過按下按鍵(或者敲下命令)來獲取,使用它們吧!
若是你是個 PHP 程序員,如今有個好機會去至少了解 HTML、CSS、JavaScript 和 MySQL。當你可以更好地處理這些語言時,就是從新學習 JavaScript 的時機了。JavaScript 並非 jQuery,你應該合理地學習 JavaScript 來更高效地使用它。
我也打算向你推薦學習面向對象的 PHP,它能夠節省時間,而且在代碼規模更大時會變得更好。對於相似 C# 和 Java 的語言,在你瞭解 OOP 以後,它們也更易於理解。
經過了解包管理器、構建腳本、CoffeeScript、LESS、SASS、YAML 、腳本引擎和其它強大的工具來擴展你的知識面。我強烈向你推薦看一看其它框架,尤爲是 Laravel。
當你使用它們出色完成任務時,學習 Ruby、RoR、Android、iPhone 和 Windows Phone 應用開發如何?你可能會認爲這毫無心義,由於它們在你的溫馨區和工做所需範圍以外,可是這就是它們的意義。每種語言都有一些要學習的實用的東西,以及從沒碰到的新知識。全部 PHP 頂級開發者都懂得不少其它編程語言,這並不是偶然。
------------------
PHP加密和解密函數能夠用來加密一些有用的字符串存放在數據庫裏,而且經過可逆解密字符串,該函數使用了base64和MD5加密和解密。
function encryptDecrypt($key, $string, $decrypt){ if($decrypt){ $decrypted = rtrim(mcrypt_decrypt(MCRYPT_RIJNDAEL_256, md5($key), base64_decode($string), MCRYPT_MODE_CBC, md5(md5($key))), "12"); return $decrypted; }else{ $encrypted = base64_encode(mcrypt_encrypt(MCRYPT_RIJNDAEL_256, md5($key), $string, MCRYPT_MODE_CBC, md5(md5($key)))); return $encrypted; } }
使用方法以下:
//如下是將字符串「Helloweba歡迎您」分別加密和解密 //加密: echo encryptDecrypt('password', 'Helloweba歡迎您',0); //解密: echo encryptDecrypt('password', 'z0JAx4qMwcF+db5TNbp/xwdUM84snRsXvvpXuaCa4Bk=',1);
當咱們須要生成一個隨機名字,臨時密碼等字符串時能夠用到下面的函數:
function generateRandomString($length = 10) { $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; $randomString = ''; for ($i = 0; $i < $length; $i++) { $randomString .= $characters[rand(0, strlen($characters) - 1)]; } return $randomString; }
使用方法以下:
echo generateRandomString(20);
如下函數能夠快速獲取文件的擴展名即後綴。
function getExtension($filename){ $myext = substr($filename, strrpos($filename, '.')); return str_replace('.','',$myext); }
使用方法以下:
$filename = '個人文檔.doc'; echo getExtension($filename);
如下使用的函數能夠獲取文件的大小,而且轉換成便於閱讀的KB,MB等格式。
function formatSize($size) { $sizes = array(" Bytes", " KB", " MB", " GB", " TB", " PB", " EB", " ZB", " YB"); if ($size == 0) { return('n/a'); } else { return (round($size/pow(1024, ($i = floor(log($size, 1024)))), 2) . $sizes[$i]); } }
使用方法以下:
$thefile = filesize('test_file.mp3'); echo formatSize($thefile);
有時咱們須要將字符串、模板標籤替換成指定的內容,能夠用到下面的函數:
function stringParser($string,$replacer){ $result = str_replace(array_keys($replacer), array_values($replacer),$string); return $result; }
使用方法以下:
$string = 'The {b}anchor text{/b} is the {b}actual word{/b} or words used {br}to describe the link {br}itself'; $replace_array = array('{b}' => '<b>','{/b}' => '</b>','{br}' => '<br />'); echo stringParser($string,$replace_array);
若是你想列出目錄下的全部文件,使用如下代碼便可:
function listDirFiles($DirPath){ if($dir = opendir($DirPath)){ while(($file = readdir($dir))!== false){ if(!is_dir($DirPath.$file)) { echo "filename: $file<br />"; } } } }
使用方法以下:
listDirFiles('home/some_folder/');
如下函數能夠獲取當前頁面的URL,無論是http仍是https。
function curPageURL() { $pageURL = 'http'; if (!empty($_SERVER['HTTPS'])) {$pageURL .= "s";} $pageURL .= "://"; if ($_SERVER["SERVER_PORT"] != "80") { $pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"]; } else { $pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"]; } return $pageURL; }
使用方法以下:
echo curPageURL();
有時咱們不想讓瀏覽器直接打開文件,如PDF文件,而是要直接下載文件,那麼如下函數能夠強制下載文件,函數中使用了application/octet-stream頭類型。
function download($filename){ if ((isset($filename))&&(file_exists($filename))){ header("Content-length: ".filesize($filename)); header('Content-Type: application/octet-stream'); header('Content-Disposition: attachment; filename="' . $filename . '"'); readfile("$filename"); } else { echo "Looks like file does not exist!"; } }
使用方法以下:
download('/down/test_45f73e852.zip');
咱們常常會遇到須要截取字符串(含中文漢字)長度的狀況,好比標題顯示不能超過多少字符,超出的長度用…表示,如下函數能夠知足你的需求。
/* Utf-八、gb2312都支持的漢字截取函數 cut_str(字符串, 截取長度, 開始長度, 編碼); 編碼默認爲 utf-8 開始長度默認爲 0 */ function cutStr($string, $sublen, $start = 0, $code = 'UTF-8'){ if($code == 'UTF-8'){ $pa = "/[\x01-\x7f]|[\xc2-\xdf][\x80-\xbf]|\xe0[\xa0-\xbf][\x80-\xbf]|[\xe1-\xef][\x80-\xbf][\x80-\xbf]|\xf0[\x90-\xbf][\x80-\xbf][\x80-\xbf]|[\xf1-\xf7][\x80-\xbf][\x80-\xbf][\x80-\xbf]/"; preg_match_all($pa, $string, $t_string); if(count($t_string[0]) - $start > $sublen) return join('', array_slice($t_string[0], $start, $sublen))."..."; return join('', array_slice($t_string[0], $start, $sublen)); }else{ $start = $start*2; $sublen = $sublen*2; $strlen = strlen($string); $tmpstr = ''; for($i=0; $i<$strlen; $i++){ if($i>=$start && $i<($start+$sublen)){ if(ord(substr($string, $i, 1))>129){ $tmpstr.= substr($string, $i, 2); }else{ $tmpstr.= substr($string, $i, 1); } } if(ord(substr($string, $i, 1))>129) $i++; } if(strlen($tmpstr)<$strlen ) $tmpstr.= "..."; return $tmpstr; } }
使用方法以下:
$str = "jQuery插件實現的加載圖片和頁面效果"; echo cutStr($str,16);
咱們常常要用數據庫記錄用戶的IP,如下代碼能夠獲取客戶端真實的IP:
//獲取用戶真實IP function getIp() { if (getenv("HTTP_CLIENT_IP") && strcasecmp(getenv("HTTP_CLIENT_IP"), "unknown")) $ip = getenv("HTTP_CLIENT_IP"); else if (getenv("HTTP_X_FORWARDED_FOR") && strcasecmp(getenv("HTTP_X_FORWARDED_FOR"), "unknown")) $ip = getenv("HTTP_X_FORWARDED_FOR"); else if (getenv("REMOTE_ADDR") && strcasecmp(getenv("REMOTE_ADDR"), "unknown")) $ip = getenv("REMOTE_ADDR"); else if (isset ($_SERVER['REMOTE_ADDR']) && $_SERVER['REMOTE_ADDR'] && strcasecmp($_SERVER['REMOTE_ADDR'], "unknown")) $ip = $_SERVER['REMOTE_ADDR']; else $ip = "unknown"; return ($ip); }
使用方法以下:
echo getIp();
咱們在查詢數據庫時,出於安全考慮,須要過濾一些非法字符防止SQL惡意注入,請看一下函數:
function injCheck($sql_str) { $check = preg_match('/select|insert|update|delete|\'|\/\*|\*|\.\.\/|\.\/|union|into|load_file|outfile/', $sql_str); if ($check) { echo '非法字符!!'; exit; } else { return $sql_str; } }
使用方法以下:
echo injCheck('1 or 1=1');
咱們在進行表單操做時,有時爲了友好須要提示用戶操做結果,並跳轉到相關頁面,請看如下函數:
function message($msgTitle,$message,$jumpUrl){ $str = '<!DOCTYPE HTML>'; $str .= '<html>'; $str .= '<head>'; $str .= '<meta charset="utf-8">'; $str .= '<title>頁面提示</title>'; $str .= '<style type="text/css">'; $str .= '*{margin:0; padding:0}a{color:#369; text-decoration:none;}a:hover{text-decoration:underline}body{height:100%; font:12px/18px Tahoma, Arial, sans-serif; color:#424242; background:#fff}.message{width:450px; height:120px; margin:16% auto; border:1px solid #99b1c4; background:#ecf7fb}.message h3{height:28px; line-height:28px; background:#2c91c6; text-align:center; color:#fff; font-size:14px}.msg_txt{padding:10px; margin-top:8px}.msg_txt h4{line-height:26px; font-size:14px}.msg_txt h4.red{color:#f30}.msg_txt p{line-height:22px}'; $str .= '</style>'; $str .= '</head>'; $str .= '<body>'; $str .= '<div>'; $str .= '<h3>'.$msgTitle.'</h3>'; $str .= '<div>'; $str .= '<h4>'.$message.'</h4>'; $str .= '<p>系統將在 <span style="color:blue;font-weight:bold">3</span> 秒後自動跳轉,若是不想等待,直接點擊 <a href="{$jumpUrl}">這裏</a> 跳轉</p>'; $str .= "<script>setTimeout('location.replace(\'".$jumpUrl."\')',2000)</script>"; $str .= '</div>'; $str .= '</div>'; $str .= '</body>'; $str .= '</html>'; echo $str; }
使用方法以下:
message('操做提示','操做成功!','http://www.helloweba.com/');
咱們在處理時間時,須要計算當前時間距離某個時間點的時長,如計算客戶端運行時長,一般用hh:mm:ss表示。
function changeTimeType($seconds) { if ($seconds > 3600) { $hours = intval($seconds / 3600); $minutes = $seconds % 3600; $time = $hours . ":" . gmstrftime('%M:%S', $minutes); } else { $time = gmstrftime('%H:%M:%S', $seconds); } return $time; }
----------------------
儘管PHP常常被人詬病,被人貶低,被人當玩笑開,事實證實,PHP是全世界網站開發中使用率最高的編程語言。PHP最大的缺點是太簡單,語法不嚴謹,框架體系很弱,但這也是它最大的優勢,一個有點編程背景的普通人,只須要學習PHP半天時間,就能夠上手開始開發web應用了。
網上有人總結幾種編程語言的特色,我以爲也挺有道理的:
PHP 就是: Quick and Dirty
Java 就是: Beauty and Slowly
Ruby 就是: Quick and Beauty
python 就是: Quick and Simple
在PHP的流行普及中,網上總結出了不少實用的PHP代碼片斷,這些代碼片斷在當你遇到相似的問題時,粘貼過去就可使用,很是的高效,很是的省時省力。將這些程序員前輩總結出的優秀代碼放到本身的知識庫中,是一個善於學習的程序員的好習慣。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
|
function
is_spam(
$text
,
$file
,
$split
=
':'
,
$regex
= false){
$handle
=
fopen
(
$file
,
'rb'
);
$contents
=
fread
(
$handle
,
filesize
(
$file
));
fclose(
$handle
);
$lines
=
explode
(
"n"
,
$contents
);
$arr
=
array
();
foreach
(
$lines
as
$line
){
list(
$word
,
$count
) =
explode
(
$split
,
$line
);
if
(
$regex
)
$arr
[
$word
] =
$count
;
else
$arr
[preg_quote(
$word
)] =
$count
;
}
preg_match_all(
"~"
.implode(
'|'
,
array_keys
(
$arr
)).
"~"
,
$text
,
$matches
);
$temp
=
array
();
foreach
(
$matches
[0]
as
$match
){
if
(!in_array(
$match
,
$temp
)){
$temp
[
$match
] =
$temp
[
$match
] + 1;
if
(
$temp
[
$match
] >=
$arr
[
$word
])
return
true;
}
}
return
false;
}
$file
=
'spam.txt'
;
$str
=
'This string has cat, dog word'
;
if
(is_spam(
$str
,
$file
))
echo
'this is spam'
;
else
echo
'this is not spam'
;
|
ab:3 dog:3 cat:2 monkey:2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
function
randomColor() {
$str
=
'#'
;
for
(
$i
= 0 ;
$i
< 6 ;
$i
++) {
$randNum
= rand(0 , 15);
switch
(
$randNum
) {
case
10:
$randNum
=
'A'
;
break
;
case
11:
$randNum
=
'B'
;
break
;
case
12:
$randNum
=
'C'
;
break
;
case
13:
$randNum
=
'D'
;
break
;
case
14:
$randNum
=
'E'
;
break
;
case
15:
$randNum
=
'F'
;
break
;
}
$str
.=
$randNum
;
}
return
$str
;
}
$color
= randomColor();
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
|
set_time_limit(0);
// Supports all file types
// URL Here:
$pi
=
pathinfo
(
$url
);
$ext
=
$pi
[
'extension'
];
$name
=
$pi
[
'filename'
];
// create a new cURL resource
$ch
= curl_init();
// set URL and other appropriate options
curl_setopt(
$ch
, CURLOPT_URL,
$url
);
curl_setopt(
$ch
, CURLOPT_HEADER, false);
curl_setopt(
$ch
, CURLOPT_BINARYTRANSFER, true);
curl_setopt(
$ch
, CURLOPT_AUTOREFERER, true);
curl_setopt(
$ch
, CURLOPT_FOLLOWLOCATION, true);
curl_setopt(
$ch
, CURLOPT_RETURNTRANSFER, true);
// grab URL and pass it to the browser
$opt
= curl_exec(
$ch
);
// close cURL resource, and free up system resources
curl_close(
$ch
);
$saveFile
=
$name
.
'.'
.
$ext
;
if
(preg_match(
"/[^0-9a-z._-]/i"
,
$saveFile
))
$saveFile
= md5(microtime(true)).
'.'
.
$ext
;
$handle
=
fopen
(
$saveFile
,
'wb'
);
fwrite(
$handle
,
$opt
);
fclose(
$handle
);
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
|
function
page_rank(
$page
,
$type
=
'alexa'
){
switch
(
$type
){
case
'alexa'
:
$handle
=
fopen
(
$url
.
$page
,
'r'
);
break
;
case
'google'
:
break
;
}
$content
= stream_get_contents(
$handle
);
fclose(
$handle
);
$content
= preg_replace(
"~(n|t|ss+)~"
,
''
,
$content
);
switch
(
$type
){
case
'alexa'
:
if
(preg_match(
'~<div class="data (down|up)"><img.+?>(.+?) </div>~im'
,
$content
,
$matches
)){
return
$matches
[2];
}
else
{
return
FALSE;
}
break
;
case
'google'
:
$rank
=
explode
(
':'
,
$content
);
if
(
$rank
[2] !=
''
)
return
$rank
[2];
else
return
FALSE;
break
;
default
:
return
FALSE;
break
;
}
}
// Alexa Page Rank:
echo
'Alexa Rank: '
.page_rank(
'techug.com'
);
echo
'
';
// Google Page Rank
echo
'Google Rank: '
.page_rank(
'techug.com'
,
'google'
);
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
$filename
=
$_GET
[
'file'
];
//Get the fileid from the URL
// Query the file ID
$query
= sprintf(
"SELECT * FROM tableName WHERE id = '%s'"
,mysql_real_escape_string(
$filename
));
$sql
= mysql_query(
$query
);
if
(mysql_num_rows(
$sql
) > 0){
$row
= mysql_fetch_array(
$sql
);
// Set some headers
header(
"Pragma: public"
);
header(
"Expires: 0"
);
header(
"Cache-Control: must-revalidate, post-check=0, pre-check=0"
);
header(
"Content-Type: application/force-download"
);
header(
"Content-Type: application/octet-stream"
);
header(
"Content-Type: application/download"
);
header(
"Content-Disposition: attachment; filename="
.
basename
(
$row
[
'FileName'
]).
";"
);
header(
"Content-Transfer-Encoding: binary"
);
header(
"Content-Length: "
.
filesize
(
$row
[
'FileName'
]));
@readfile(
$row
[
'FileName'
]);
exit
(0);
}
else
{
header(
"Location: /"
);
exit
;
}
|
1
2
|
echo
'<img src="'
.
$gravatar_link
.
'" />'
;
|
1
2
3
4
5
6
7
|
$ch
= curl_init();
curl_setopt(
$ch
,CURLOPT_URL,
'https://feedburner.google.com/api/awareness/1.0/GetFeedData?id=7qkrmib4r9rscbplq5qgadiiq4'
);
curl_setopt(
$ch
,CURLOPT_RETURNTRANSFER,1);
curl_setopt(
$ch
,CURLOPT_CONNECTTIMEOUT,2);
$content
= curl_exec(
$ch
);
$subscribers
= get_match(
'/circulation="(.*)"/isU'
,
$content
);
curl_close(
$ch
);
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
function
ago(
$time
)
{
$periods
=
array
(
"second"
,
"minute"
,
"hour"
,
"day"
,
"week"
,
"month"
,
"year"
,
"decade"
);
$lengths
=
array
(
"60"
,
"60"
,
"24"
,
"7"
,
"4.35"
,
"12"
,
"10"
);
$now
= time();
$difference
=
$now
-
$time
;
$tense
=
"ago"
;
for
(
$j
= 0;
$difference
>=
$lengths
[
$j
] &&
$j
<
count
(
$lengths
)-1;
$j
++) {
$difference
/=
$lengths
[
$j
];
}
$difference
=
round
(
$difference
);
if
(
$difference
!= 1) {
$periods
[
$j
].=
"s"
;
}
return
"$difference $periods[$j] 'ago' "
;
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
$filename
=
"test.jpg"
;
list(
$w
,
$h
,
$type
,
$attr
) =
getimagesize
(
$filename
);
$src_im
= imagecreatefromjpeg(
$filename
);
$src_x
=
'0'
;
// begin x
$src_y
=
'0'
;
// begin y
$src_w
=
'100'
;
// width
$src_h
=
'100'
;
// height
$dst_x
=
'0'
;
// destination x
$dst_y
=
'0'
;
// destination y
$dst_im
= imagecreatetruecolor(
$src_w
,
$src_h
);
$white
= imagecolorallocate(
$dst_im
, 255, 255, 255);
imagefill(
$dst_im
, 0, 0,
$white
);
imagecopy(
$dst_im
,
$src_im
,
$dst_x
,
$dst_y
,
$src_x
,
$src_y
,
$src_w
,
$src_h
);
header(
"Content-type: image/png"
);
imagepng(
$dst_im
);
imagedestroy(
$dst_im
);
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
function
Visit(
$url
){
$agent
=
"Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)"
;
$ch
=curl_init();
curl_setopt (
$ch
, CURLOPT_URL,
$url
);
curl_setopt(
$ch
, CURLOPT_USERAGENT,
$agent
);
curl_setopt (
$ch
, CURLOPT_RETURNTRANSFER, 1);
curl_setopt (
$ch
,CURLOPT_VERBOSE,false);
curl_setopt(
$ch
, CURLOPT_TIMEOUT, 5);
curl_setopt(
$ch
,CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt(
$ch
,CURLOPT_SSLVERSION,3);
curl_setopt(
$ch
,CURLOPT_SSL_VERIFYHOST, FALSE);
$page
=curl_exec(
$ch
);
//echo curl_error($ch);
$httpcode
= curl_getinfo(
$ch
, CURLINFO_HTTP_CODE);
curl_close(
$ch
);
if
(
$httpcode
>=200 &&
$httpcode
<300)
return
true;
else
return
false;
}
echo
"Website OK"
.
"n"
;
else
echo
"Website DOWN"
;
|