0% found this document useful (0 votes)
61 views6 pages

PHP Validation

The document discusses PHP validation, email, cookies, sessions, and MVC. It provides code examples of: 1) Validating user form input in PHP using the test_input() function to trim, strip slashes, and encode HTML special characters. 2) Sending an email with PHP using the mail() function and specifying the to, subject, message, and headers. 3) Creating, retrieving, and deleting cookies in PHP using the setcookie(), $_COOKIE, and unset() functions. 4) Starting sessions in PHP with session_start(), storing session variables in $_SESSION, and destroying sessions with session_destroy().

Uploaded by

hhh hhh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
61 views6 pages

PHP Validation

The document discusses PHP validation, email, cookies, sessions, and MVC. It provides code examples of: 1) Validating user form input in PHP using the test_input() function to trim, strip slashes, and encode HTML special characters. 2) Sending an email with PHP using the mail() function and specifying the to, subject, message, and headers. 3) Creating, retrieving, and deleting cookies in PHP using the setcookie(), $_COOKIE, and unset() functions. 4) Starting sessions in PHP with session_start(), storing session variables in $_SESSION, and destroying sessions with session_destroy().

Uploaded by

hhh hhh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

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

<h2>PHP Form Validation Example</h2>


<form method="post" action="<?php echo htmlspecialchars
($_SERVER["PHP_SELF"]);?>">  
  Name: <input type="text" name="name">
  <br><br>
  E-mail: <input type="text" name="email">
  <br><br>
  Website: <input type="text" name="website">
  <br><br>
  Comment: <textarea name="comment" rows="5" cols="40"></textarea>
  <br><br>
  Gender:
  <input type="radio" name="gender" value="female">Female
  <input type="radio" name="gender" value="male">Male
  <input type="radio" name="gender" value="other">Other
  <br><br>
  <input type="submit" name="submit" value="Submit">  
</form>

<?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"]; ?>

If you want to see the full $_COOKIE array, you can do the following.

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.

The Function session_start() should be in starting, and you can't send


anything to the browser before it.
Example:
<?php
session_start();
?>

<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.

Storing a session variable


$_SESSION[] is an associative array, which is used to store information is a session.
These variables can be accessed during the lifetime of a session.

Example:
<?php
session_start();

// store session data


$_SESSION["username"] = "nikita";
$_SESSION["email"] = "nikita@example.com";

//retrieve session data


echo $_SESSION["username"];
echo "<br>";
echo $_SESSION["email"];
?>

Destroying a session.

A complete PHP session can be terminated by using session_destroy() function, which


does not require any arguments.

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

How MVC Flow?


First user interact with controller. Now controller will call appropriate
Model and Model return the data and now controller pass this data to
appropriate user.

You might also like