Php 这里怎么了?(新手)

Php 这里怎么了?(新手),php,Php,在实际发送电子邮件之前,我需要检查是否有互联网连接。在添加以下代码和函数之前,一切正常: if(doesConnectionExist()) { $errors .= "\n No internet connection!"; } function doesConnectionExist { var xhr = new XMLHttpRequest(); var file = "http://www.?????.com/somefile.png"; var ra

在实际发送电子邮件之前,我需要检查是否有互联网连接。在添加以下代码和函数之前,一切正常:

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;
    }
}
加上这个功能:

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 ='????@????.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;
    }
}
?>
如果有人能帮助我,那就太好了! 提前谢谢。

您的doesConnectionExist是javascript,而不是PHP=

您可以使用此帖子ping服务器并检查internet连接:

编辑:

<?php 
header ('Content-type: text/html; charset=iso8859-15');
$your_email ='????@????.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;
    }
}
?>

您错过了doesConnectionExist函数。您似乎在混合JavaScript和PHP。他们是两种完全不同的语言。Oeps!这当然是一个主要问题。。。。谢谢你的回答谢谢智库的回答。如何在代码中实现这一点?你能把它放进去吗?谢谢