Using MySQL in a Hosting Environment

MySQL is a heavy-duty database server. Used with various software languages, most notably PHP, and part of the LAMP family (which is comprised of Linux, Apache, MySQL, and PHP),

Whether it’s a customer relations management list or medical records, any data you have that needs storing, is stored on a database. You will need a database management system to add, access, edit, and process data in your databases, and MySQL is one of the best and most comprehensive packages on the market. MySQL can also be used as a relational database management system, which means it can store data in various tables that can communicate with one another rather than all in one large “storeroom” of data. Continue reading “Using MySQL in a Hosting Environment”

How to send Push Notifications using PHP / Google Android

Google Cloud Messaging (GCM) for Android is a service that allows you to send data from your server to your users’ Android-powered device, and also to receive messages from devices on the same connection. The GCM service handles all aspects of queueing of messages and delivery to the target Android application running on the target device, and it is completely free.

The only thing you will need is a Google_Api_key:

To create a Google API project:

  1. Open the Google Developers Console.
  2. If you haven’t created an API project yet, click Create Project.
  3. Supply a project name and click Create.Once the project has been created, a page appears that displays your project ID and project number. For example, Project Number: 670330094152.
  4. Copy down your project number. You will use it later on as the GCM sender ID.

To obtain an API key:

  1. In the sidebar on the left, select APIs & auth > Credentials.
  2. Under Public API access, click Create new key.
  3. In the Create a new key dialog, click Server key.
  4. In the resulting configuration dialog, supply your server’s IP address. For testing purposes, you can use0.0.0.0/0.
  5. Click Create.
  6. In the refreshed page, copy the API key. You will need the API key later on to perform authentication in your app server.

Continue reading “How to send Push Notifications using PHP / Google Android”

How to check for HTML content in contact forms? Spam proof without Captcha.

If you ever wanted to stop receiving spam about luis vuitton bags, you wondered how you can spam-proof your contact page without the addition of captchas.

The contact form HTML Code (make sure you include max length for all the fields:

<form name=”contactform” id=”contactform” method=”post” action=”send_form_email.php”>
<ul class=”row form”><li class=”col left”>
<input type=”text” id=”Name” name=”Name” class=”required” placeholder=”Name” maxlength=”100″>
<input type=”text” id=”Email” name=”Email” class=”required” placeholder=”Email” maxlength=”100″>
<input type=”text” id=”Subject” name=”Subject” class=”” placeholder=”Subject” maxlength=”50″></li>
<li class=”col right”><textarea id=”Message” name=”Message” placeholder=”Message”></textarea></li></ul>

</form>

The PHP code in send_form_email.php:

<?php
function spamcheck($field)
{
    //filter_var() sanitizes the e-mail 
    //address using FILTER_SANITIZE_EMAIL 
    $field = filter_var($field, FILTER_SANITIZE_EMAIL);
    
    //filter_var() validates the e-mail 
    //address using FILTER_VALIDATE_EMAIL 
    if (filter_var($field, FILTER_VALIDATE_EMAIL)) {
        return TRUE;
    } else {
        return FALSE;
    }
}

function linkcheck($message)
{
    //$message = 'Check this out <a href="http://www.something.com">Click here</a>. Click it';
    
    if (preg_match('/<a[s]+[^>]*?href[s]?=[s""']+(.*?)[""']+.*?>([^<]+|.*?)?</a>/', $message)) {
        // THERE IS A HYPERLINK IN THE MESSAGE
        // DO SOMETHING
        return TRUE;
    } else {
        return FALSE;
    }
}

function clean_string($string)
{
    $bad = array(
        "content-type",
        "bcc:",
        "to:",
        "cc:",
        "href"
    );
    return str_replace($bad, "", $string);
}


function died($error)
{
    
    // your error code can go here
    
    echo "We are very sorry, but there were error(s) found with the form you submitted. ";
    echo "These errors appear below.<br /><br />";
    echo $error . "<br /><br />";
    echo "Please go back and fix these errors.<br /><br />";
    die();
    
}


if (isset($_POST['Email'])) {
    
    //check if the email address is invalid 
    $mailcheck = spamcheck($_POST['Email']);
    if ($mailcheck == FALSE) {
        died("The Email Address you entered does not appear to be valid.");
    }
    //send email 
    $email_to      = "YOUR EMAIL";
    $email_subject = "Query Submitted ";
    
    // validation expected data exists
    
    if (!isset($_POST['Name']) || !isset($_POST['Subject']) || !isset($_POST['Email']) || !isset($_POST['Message'])) {
        
        died('We are sorry, but there appears to be a problem with the form you submitted. Please make sure all fields are filled in!');
    }
    
    $first_name = $_POST['Name']; // required
    $email      = $_POST['Email']; // required
    $subject    = $_POST['Subject']; // not required
    $comments   = $_POST['Message']; // required
    
    
    $error_message = "";
    $string_exp    = "/^[A-Za-z .'-]+$/";
    if (!preg_match($string_exp, $first_name)) {
        $error_message .= 'The Name you entered does not appear to be valid.<br />';
    }
    
    if ((strlen($comments) < 2) || (linkcheck($comments) == TRUE)) {
        $error_message .= 'The Message you entered do not appear to be valid.<br />';
    }
    if ((strlen($subject) < 2) || (linkcheck($subject) == TRUE)) {
        $error_message .= 'The Subject you entered do not appear to be valid.<br />';
    }
    
    if (strlen($error_message) > 0) {
        
        died($error_message);
        
    }
    
    $email_message = "Dear AdventExhibitions Administrator,nn A new query has been submitted on the website. Details below: nn";
    
    $email_subject .= $subject;
    $email_message .= "Name: " . clean_string($first_name) . "n";
    $email_message .= "Subject: " . clean_string($subject) . "n";
    $email_message .= "Email: " . clean_string($email) . "n";
    $email_message .= "Comments: " . clean_string($comments) . "n";
    
    
    // create email headers
    
    $headers = 'From: YOUR EMAIL' . "rn" . 'Reply-To: YOUR EMAIL' . "rn" . 'X-Mailer: PHP/' . phpversion();
    
    
    mail($email_to, $email_subject, $email_message, $headers);
    
?>
 
Thank you for contacting us. We will be in touch with you very soon.
 
 <?php
}
?>

How well do you know PHP?

phpQ1: What is the output:

$x = 42;
echo isset($x);

Q2: What is the output:

$x = 1;
echo is_null($x);

Q3: Which Comments are legal?:

Q4: Are PHP variables case sensitive?

Q5: What is the output?

$a = round(4.123456, 3);
echo $a;

Q6: Can I run a php script from the command line?

Q7: What is the output?

echo ord(‘a’);

Q8: Can I get the screen resolution with php?

Q9: What is the output?

$sting = ‘I love Windows’;
echo str_replace(“Windows”, “Linux”, $string);

Q10: What is the output of the MySQL query below?

SET @str = BINARY ‘New York’;
SELECT LOWER(@str), LOWER(CONVERT(@str USING latin1));

Q11: What is the output of the MySQL query below?

SELECT RTRIM(‘barbar ‘);

Q12: What is the output of the MySQL query below?

SELECT RPAD(‘hi’,5,’?’);

Q13: What is the output of the MySQL query below?

SELECT SUBSTRING(‘Sakila’ FROM -4 FOR 2);

Q14: What is the output of the MySQL query below?

SELECT SUBSTRING(‘Quadratically’,5,6);

Q15: What is the output of the MySQL query below?

SELECT ‘2010-12-31 23:59:59’ + INTERVAL 1 SECOND;

Bonus Question:

$x=glob($path.’*’, GLOB_MARK|GLOB_ONLYDIR|GLOB_NOSORT);

What does the code above do?