PHP Day 3

$_GET is an array of variables passed to the current script via the URL parameters.

$_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');

?>



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值