-2

I need to check if there is an internet-connection before actually sending the email. Everything works fine until I add this code and function:

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

plus this function:

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

The total code looks like this:

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

If anybody could help me would be absolutely great! Thank you in advance.

Erwin
  • 1
  • 1

1 Answers1

0

Your doesConnectionExist is javascript, not PHP =)

You can use this post to ping the server and check internet connection :

StackOverflow : Ping IP addresses

EDIT :

<?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;
    }
}
?>
Community
  • 1
  • 1
ThinkTank
  • 1,187
  • 9
  • 15
  • Thank you ThinkTank for your answer. How do I implement this in my code? Is it possible you put it in? Thanks – Erwin Mar 30 '16 at 13:26