$_POST is an array of variables passed to the current script via the HTTP POST method.
PHP - Validate Name
$name = test_input($_POST["name"]);
if (!preg_match("/^[a-zA-Z ]*$/",$name)) {
$nameErr = "Only letters and white space allowed";
}
PHP - Validate E-mail
$email = test_input($_POST["email"]);
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$emailErr = "Invalid email format";
}
PHP - Validate URL
$website = test_input($_POST["website"]);
if (!preg_match("/\b(?:(?:https?|ftp):\/\/|www\.)[-a-z0-9+&@#\/%?=~_|!:,.;]*[-a-z0-9+&@#\/%=~_|]/i",$website)) {
$websiteErr = "Invalid URL";
}
date() function formats a timestamp to a more readable date and time.
date(format,timestamp)
strtotime() function is used to convert a human readable string to a Unix time.
The Unix timestamp contains the number of seconds between the Unix Epoch (January 1 1970 00:00:00 GMT) and the time specified.
PHP include and require Statements
Use require when the file is required by the application.
Use include when the file is not required and application should continue when file is not found.
localhost:8088/tutorial/upload.html
A cookie is often used to identify a user. A cookie is a small file that the server embeds on the user's computer. Each time the same computer requests a page with a browser, it will send the cookie too.
The setcookie() function must appear BEFORE the <html> tag.
A session is a way to store information (in variables) to be used across multiple pages.
PHP first creates a unique identifier for that particular session which is a random string of 32 hexadecimal numbers such as 3c7foj34c3jj973hjkop2fc937e3443.
A cookie called PHPSESSID is automatically sent to the user's computer to store unique session identification string.
A file is automatically created on the server in the designated temporary directory and bears the name of the unique identifier prefixed by sess_ ie sess_3c7foj34c3jj973hjkop2fc937e3443.
filter_var() function filters a variable with the specified filter.
filter_var(var, filtername, options)
var: Required. The variable to filter
filtername: Optional. Specifies the ID or name of the filter to use. Default is FILTER_DEFAULT, which results in no filtering
options: Optional. Specifies one or more flags/options to use. Check each filter for possible options and flags
// Remove all illegal characters "SANITIZE"
$email = filter_var($email, FILTER_SANITIZE_EMAIL);
//Validate email "VALIDATE"
if (!filter_var($email, FILTER_VALIDATE_EMAIL) === false) {
echo("$email is a valid email address");
} else {
echo("$email is not a valid email address");
}
Creating a Custom Error Handler
error_function(error_level,error_message, error_file,error_line,error_context)
set_error_handler() function sets a user-defined error handler function.
set_error_handler(errorhandler,E_ALL|E_STRICT);
set_exception_handler() function sets a user-defined function to handle all uncaught exceptions:
<?php
function myException($exception) {
echo "<b>Exception:</b> " . $exception->getMessage();
}
set_exception_handler('myException');
?>