2016-03-30 20 views
-2

Aslında e-postayı göndermeden önce bir internet bağlantısı olup olmadığını kontrol etmem gerekiyor.Burada neyin var? (newbie)

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

artı bu işlevi: toplam kod şöyle

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

: Bu kodu ve işlev eklemek kadar her şey gayet iyi çalışıyor

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

herkes beni olacağını yardım edebilirsen kesinlikle harika! Önceden teşekkür ederiz. Sizin doesConnectionExist

+1

cevapsız() işlevinde "doesConnectionExist". –

+3

JavaScript ve PHP'yi karıştırıyor gibi görünüyorsunuz. Tamamen farklı iki dil var. – David

+0

Oeps! Tabii ki bu bir ana konudur .... Cevabınız için teşekkürler – Erwin

cevap

0

Sen sunucuya ping ve internet bağlantısı kontrol etmek için bu yazıyı kullanabilirsiniz

javascript, değil PHP =) 'dir:

StackOverflow : Ping IP addresses

DÜZENLEME:

<?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

Cevabınız için teşekkür ederiz. Bunu kodumda nasıl uygularım? Mümkün mü? Teşekkürler – Erwin

+0

Kod ile düzenle! – ThinkTank