2016-03-30 103 views
-2

我需要在實際發送電子郵件之前檢查是否有互聯網連接。這裏有什麼問題? (新手)

if(doesConnectionExist()) 
{ 
    $errors .= "\n No internet connection!"; 
} 

加上這個功能:

function doesConnectionExist 
{ 
    var xhr = new XMLHttpRequest(); 
    var file = "http://www.?????.com/somefile.png"; 
    var randomNum = Math.round(Math.random() * 10000); 
    xhr.open('HEAD', file + "?rand=" + randomNum, false); 
    try { 
     xhr.send(); 
     if (xhr.status >= 200 && xhr.status < 304) { 
      return true; 

     } else { 
      return false; 
     } 
    } catch (e) { 
     return false; 
    } 
} 

總代碼如下所示:直到我加入這樣的代碼和功能一切正常

<?php 
header ('Content-type: text/html; charset=iso8859-15'); 
$your_email ='[email protected]????.com'; 
session_start(); 
$errors = ''; 
$firstname = ' '; 
$lastname = ''; 
$visitor_email = ''; 

if(isset($_POST['submit'])) 
{ 
$firstname = $_POST['firstname']; 
$lastname = $_POST['lastname']; 
$visitor_email = $_POST['email']; 

if(empty($firstname)||empty($lastname) 
{ 
    $errors .= "\n firstname and lastname are required fields. "; 
} 


if(doesConnectionExist()) 
{ 
    $errors .= "\n No internet connection!"; 
} 


if(empty($errors)) 
{ 
    $to = $your_email; 
    $subject="test"; 
    $from = $your_email; 
    $body = "test\n". 
    "Firstname: $firstname\n". 
    "Lastname: $lastname \n". 
    $headers = "Reply-To: $visitor_email \r\n"; 
    mail($to, $subject, $body, $headers); 
    header('Location: thankyou.html'); 
} 
} 

function doesConnectionExist 
    { 
    var xhr = new XMLHttpRequest(); 
    var file = "http://www.?????.com/somefile.png"; 
    var randomNum = Math.round(Math.random() * 10000); 
    xhr.open('HEAD', file + "?rand=" + randomNum, false); 
    try { 
     xhr.send(); 
     if (xhr.status >= 200 && xhr.status < 304) { 
      return true; 

     } else { 
      return false; 
     } 
    } catch (e) { 
     return false; 
    } 
} 
?> 

如果有人可以幫助我將絕對好! 預先感謝您。

+1

你錯過()在函數「doesConnectionExist」中。 –

+3

您似乎在混合使用JavaScript和PHP。他們是兩種完全不同的語言。 – David

+0

Oeps! 這當然是一個主要問題.... 感謝您的回答 – Erwin

回答

0

doesConnectionExist是JavaScript的,而不是PHP =)

你可以用這個帖子來ping服務器並檢查網絡連接:

StackOverflow : Ping IP addresses

編輯:

<?php 
header ('Content-type: text/html; charset=iso8859-15'); 
$your_email ='[email protected]????.com'; 
session_start(); 
$errors = ''; 
$firstname = ' '; 
$lastname = ''; 
$visitor_email = ''; 

if(isset($_POST['submit'])) 
{ 
$firstname = $_POST['firstname']; 
$lastname = $_POST['lastname']; 
$visitor_email = $_POST['email']; 

if(empty($firstname)||empty($lastname) 
{ 
    $errors .= "\n firstname and lastname are required fields. "; 
} 


if(doesConnectionExist('http://www.?????.com/somefile.png')) 
{ 
    $errors .= "\n No internet connection!"; 
} 


if(empty($errors)) 
{ 
    $to = $your_email; 
    $subject="test"; 
    $from = $your_email; 
    $body = "test\n". 
    "Firstname: $firstname\n". 
    "Lastname: $lastname \n". 
    $headers = "Reply-To: $visitor_email \r\n"; 
    mail($to, $subject, $body, $headers); 
    header('Location: thankyou.html'); 
} 
} 

function doesConnectionExist($host) 
{ 
    exec("ping -c 4 " . $host, $output, $result); 

    if ($result == 0) 
    { 
     return true; 
    } 
    else 
    { 
     return false; 
    } 
} 
?> 
+0

謝謝ThinkTank的答案。 如何在我的代碼中實現這一點? 你有可能把它放進去嗎? 謝謝 – Erwin

+0

用代碼編輯! – ThinkTank