PHP Validation
PHP Validation
<!DOCTYPE HTML>
<html>
<head>
</head>
<body>
<?php
// define variables and set to empty values
$name = $email = $gender = $comment = $website = "";
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$name = test_input($_POST["name"]);
$email = test_input($_POST["email"]);
$website = test_input($_POST["website"]);
$comment = test_input($_POST["comment"]);
$gender = test_input($_POST["gender"]);
}
function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
?>
<?php
echo "<h2>Your Input:</h2>";
echo $name;
echo "<br>";
echo $email;
echo "<br>";
echo $website;
echo "<br>";
echo $comment;
echo "<br>";
echo $gender;
?>
</body>
</html>
PHP EMAIL
<?php
$to = "someone@example.com";
$subject = "Test mail";
$message = "Hello! This is a test email message.";
$from = "me@example.com";
$headers = "From:" . $from;
mail($to,$subject,$message,$headers)
?>
PHP COOKIE & SESSION
Php cookie
Create a cookie
PHP offers various functions according to the purposes, and one of them is setcookie() which is
used to set a cookie on the client machine.
SYNTAX:
setcookie(name, value, expiration);
$name - a name of the cookie. Example: "username"
$value - a value of the cookie. Example: "Jhon"
$expire - time (in UNIX timestamp) when the cookie will expire. Example: time()
+"3600". A cookie is set to expire after one hour.
RETRIEVE a cookie
PHP $_COOKIE is an automatic global or 'superglobal' variable that retrieves the cookie value.
Example:
<?php echo $_COOKIE["username"]; ?>
Example:
<pre>
<?php print_r($_COOKIE); ?>
</pre>
Delete a cookie
To delete a cookie, you just need to set that cookie to a previous date. The following
PHP code will delete the cookie we set in the above example.
Example:
<?php setcookie("username", "", time()-3600); ?>
or
<?php setcookie('username', null, -1, '/'); ?>
Php session
Start a session.
Before use session variables, you must first start the session using
the session_start() function.
<html>
<body>
</body>
</html>
When you start a session a unique session id (PHPSESSID) for each
visitor/user is created. You can access the session id using the
PHP session_id() function or predefined constant PHPSESSID.
Example:
<?php
session_start();
Destroying a session.
Example:
<?php
session_start();
session_destroy();
?>
If you wish to delete a single session variable, then you can use unset() function.
Example:
<?php
session_start();
if(isset($_SESSION[username]))
unset($_SESSION[username]);
?>
MVC in PHP
What is MVC?
MVC is a framework, it is stand for Model View Controller.
M:- Model (Database)
V:- View (Front End) UI
C:- Controller(Logical part)
What is Framework?
A framework is a set of rules which we use in order to develop a
program.
Why MV required?
- Fast development process
- Code easy to understand
- Code easy to maintain