Sunday, 1 February 2015

Update & Delete Cookies

If want to update & delete single cookie we can use the below methods ref from previous example

http://php-guidelines.blogspot.com/2015/02/cookies.html:


Delete Cookie :

setcookie("your_user", "", time() - 3600);


Update Cookie:

if user want to update cookie then set again cookie using setcookie method example:

$cookie_name = "your_user";
$cookie_value = "new value";
setcookie($cookie_name, $cookie_value, time() + (86400 * 30)); // 86400 = 1 day

Cookies

Normally cookie use to identify users . Cookie is file which save on client/user system , from where we can fetch info
syntax for cookie setcookie(name, value, expire)
Example:

<!DOCTYPE html>
<?php
$cookie_name = "your_user";
$cookie_value = "php guides";
setcookie($cookie_name, $cookie_value, time() + (86400 * 30)); // 86400 = 1 day
?>
<html>
<body>

<?php
if(!isset($_COOKIE[$cookie_name])) {
      echo "Cookie named '" . $cookie_name . "' is not set!";
} else {
      echo "Cookie '" . $cookie_name . "' is set!<br>";
      echo "Your Cookie Value is: " . $_COOKIE[$cookie_name];
}
?>

<p style="color:red">If You Don't See value reload to see cookie value</p>

</body>
</html>


Output Print



Date In Php

to get current date in php use B.I.F (Built in function) date ,Syntax for date date('format'):

<?php
//print date in year month and day format 
echo date('y-m-d');

//print output 15-02-01 current date
we can also use Year,Month & Date indivisualy
//print  day 
echo date('d');
//print  year 
echo date('y);
//print  month  
echo date('m');

?>

Url Validation

we can also validate our url by use following regular expression:

<?php
 $weburl= test_input($_POST["url"]);
if (!preg_match("/\b(?:(?:https?|ftp):\/\/|www\.)[-a-z0-9+&@#\/%?=~_|!:,.;]*[-a-z0-9+&@#\/%=~_|]/i",$weburl)) {
  $weburlErr= "Invalid URL"; 
}
?>

Email Validation

If user enter invalid email we can validate it using filter_var() function using previous example
http://php-guidelines.blogspot.com/2015/02/form-validation.html:

replace following if statement  of email :

<?php
 if (empty($_POST["email"])) {
    $emailErr = "Email is required";
  } else {
    $email = test_input($_POST["email"]);
    // check if e-mail address is valid or not
    if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
      $emailErr = "Invalid email format"; 
    }
  }

?>

Output Print


Form Validation

Form validation is necessary in many cases if user filling signup form than some fields are must be required on server side to handle this type of situations we use form validation.

<!DOCTYPE HTML> 
<html>
<head>
<style>
.error {color: #FF0000;}
</style>
</head>
<body> 

<?php
// define variables and set to empty values
$usernameErr = $emailErr = $infoErr = "";
$username = $email = $info = "";

if ($_SERVER["REQUEST_METHOD"] == "POST") {
   if (empty($_POST["username"])) {
     $usernameErr = "Name is required";
   } else {
     $username = test_input($_POST["username"]);
   }
   
   if (empty($_POST["email"])) {
     $emailErr = "Email is required";
   } else {
     $email = test_input($_POST["email"]);
   }
     
   if (empty($_POST["info"])) {
     $infoErr = "";
   } else {
     $info = test_input($_POST["info"]);
   }


}

function test_input($data) {
   $data = trim($data);
   $data = stripslashes($data);
   $data = htmlspecialchars($data);
   return $data;
}
?>

<h2> Form Validation Example</h2>
<p><span class="error">* required field.</span></p>
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>"> 
   Name: <input type="text" name="username">
   <span class="error">* <?php echo $usernameErr;?></span>
   <br><br>
   E-mail: <input type="text" name="email">
   <span class="error">* <?php echo $emailErr;?></span>
   <br><br>
   info: <input type="text" name="info">
   <span class="error"><?php echo $infoErr;?></span>
   <br><br>
  
   <input type="submit" name="submit" value="Submit"> 
</form>

<?php
echo "<h2>Output Print:</h2>";
echo $username;
echo "<br>";
echo $email;
echo "<br>";
echo $info;
echo "<br>";
?>

</body>
</html>




Output Print

Required fields highlighted