PHP7 Tutorial | Part 2 – GET, POST and Cookies

php7 tutorial - get post and cookies

PHP7 Tutorial – GET POST and Cookies
In this tutorial, we use $_GET, $_POST, and $_COOKIE superglobal to collect data

What is HTML?

HTML (HyperText Markup Language) is developed by Tim Berners-Lee . An HTML document which can be displayed through a web browser is called a web page. HTML is not a computer language it’s a markup language.

For more information: HTML Tutorial : Introduction for beginners (Basic)

What is PHP?

PHP: Hypertext Preprocessor is a general purpose server side script language. it designed for web development. It designed by Rasmus Lerdorf in 1994 and first released in 1995. PHP is free to download and use. It can contain text, HTML, JavaScript, CSS and PHP code. it runs on Windows, Linux, Mac OS and etc… PHP can do many things such as generate dynamic web pages, manage data in your database, control user access, etc… PHP 7 is much faster than the previous versions. In this tutorial. You will learn to install and run PHP on Windows pc.

For more information: PHP 7 Tutorial: Introduction for Beginners (Basic)

PHP7 – GET Method

In this method all information visible to others. all data are display in the URL. GET should be used for sending non-sensitive information. This method can’t send up to 1,024 characters. GET can’t be used to send images, documents, or media, (binary data) to the server.

http://domain.com/index.htm?name1=value1&name2=value2&name3=value3

Example 1:

HTML File (index.html)

<html>
<body>

<form action="showdata.php" method="GET">

First Name: <input type="text" name="fname" required/>
<br><br>
Last Name: <input type="text" name="lname"/>
<br><br>
E-mail: <input type="text" name="email" required>
<br><br>
<textarea name="msg" name="msg"> </textarea>
<br><br>
<input type="submit" text="Submit">
</form>

</body>
</html>

PHP File (showdata.php)

<html>
<body>

<h2><center> Welcome, <?php echo $_GET["fname"];?></h2>
<h3><center> Email: <?php echo $_GET["email"]; ?></h3>
<h3><center> Message: <?php echo $_GET["msg"]; ?></h3>

</body>
</html>
php7 tutorial - get post and cookies
GET Method – (index.html)
php7 tutorial - get post and cookies
GET Method – (showdata.php)

Example 2:

HTML File (index.html)

<html>
<body bg-color:"red">

<form action="showdata.php" method="GET">

Gender: <br>
<input type="radio" name="gender" value="Male"/> Male
<br>
 <input type="radio" name="gender" value="Female"/> Female
<br>
<input type="radio" name="gender" value="Others"/> Others
<br><br>

Which is the best programming languages: <br>
<input type="checkbox" name="pl[]" value="Java"/> Java
<br>
<input type="checkbox" name="pl[]" value="Python"/> Python
<br>
<input type="checkbox" name="pl[]" value="PHP"/> PHP
<br>
<input type="checkbox" name="pl[]" value="C++"/> C++
<br><br>

Country: 
<select name="country">
    <option value="USA"> United State </option>
    <option value="UK"> United Kingdom </option> 
    <option value="CANADA"> Canada </option>
    <option value="CHINA"> China </option>
</select>
<br><br>

Which is the best programming language: <br>
<input list="prolang" name="prolang">
 <datalist id="prolang">
  <option value="Python"></option>
  <option value="Java"></option>
  <option value="C"></option>
  <option value="C++"></option>
  <option value="PHP"></option>
 </datalist>
<br><br>
<input type="submit" text="Submit">
</form>

</body>
</html>

PHP File (showdata.php)

<html>
<body>

<p>
<center><b>Gender: </b><?php echo $_GET["gender"];?>
<p>

<p>
<center><b>Best programming language:</b>
<?php 
if(!empty($_GET['pl'])){
  foreach($_GET['pl'] as $planguge){
    echo $planguge;
    echo " / ";
  }
}
?>
</p>

<p>
<center><b>Country: </b><?php echo $_GET["country"]; ?>
</p>

<p>
<center><b>Best programming language: </b><?php echo $_GET["prolang"]; ?>
</p>

</body>
</html>
php7 tutorial - get post and cookies
GET Method – (index.html)
php7 tutorial - get post and cookies
GET Method – (showdata.php)

PHP 7 – POST Method

In this method all information invisible to others. all data are embedded within the body of the HTTP request. This method has no data sending limit. POST should be used for sending sensitive information such as login/registration details, payment details, and etc.. this method can be used to send images, documents, and media to the server.

Example 1:

HTML File (index.html)

<html>
<body>

<form action="showdata.php" method="POST">

First Name: <input type="text" name="fname" required/>
<br><br>
Last Name: <input type="text" name="lname"/>
<br><br>
E-mail: <input type="text" name="email" required>
<br><br>
<textarea name="msg" name="msg"> </textarea>
<br><br>
<input type="submit" text="Submit">
</form>

</body>
</html>

PHP File (showdata.php)

<html>
<body>

<h2><center> Welcome, <?php echo $_POST["fname"];?></h2>
<h3><center> Email: <?php echo $_POST["email"]; ?></h3>
<h3><center> Message: <?php echo $_POST["msg"]; ?></h3>

</body>
</html>
php7 tutorial - get post and cookies
POST Method – (index.html)
php7 tutorial - get post and cookies
POST Method – (showdata.php)

Example 2:

HTML File (index.html)

<html>
<body>

<form action="showdata.php" method="POST">

Gender: <br>
<input type="radio" name="gender" value="Male"/> Male
<br>
 <input type="radio" name="gender" value="Female"/> Female
<br>
<input type="radio" name="gender" value="Others"/> Others
<br><br>

Which is the best programming languages: <br>
<input type="checkbox" name="pl[]" value="Java"/> Java
<br>
<input type="checkbox" name="pl[]" value="Python"/> Python
<br>
<input type="checkbox" name="pl[]" value="PHP"/> PHP
<br>
<input type="checkbox" name="pl[]" value="C++"/> C++
<br><br>

Country: 
<select name="country">
    <option value="USA"> United State </option>
    <option value="UK"> United Kingdom </option> 
    <option value="CANADA"> Canada </option>
    <option value="CHINA"> China </option>
</select>
<br><br>

Which is the best programming language: <br>
<input list="prolang" name="prolang">
 <datalist id="prolang">
  <option value="Python"></option>
  <option value="Java"></option>
  <option value="C"></option>
  <option value="C++"></option>
  <option value="PHP"></option>
 </datalist>
<br><br>
<input type="submit" text="Submit">
</form>

</body>
</html>

PHP File (showdata.php)

<html>
<body>

<p>
<center><b>Gender: </b><?php echo $_POST["gender"];?>
<p>

<p>
<center><b>Best programming language:</b>
<?php 
if(!empty($_POST['pl'])){
  foreach($_POST['pl'] as $planguge){
    echo $planguge;
    echo " / ";
  }
}
?>
</p>

<p>
<center><b>Country: </b><?php echo $_POST["country"]; ?>
</p>

<p>
<center><b>Best programming language: </b><?php echo $_POST["prolang"]; ?>
</p>

</body>
</html>
php7 tutorial - get post and cookies
POST Method – (index.html)
php7 tutorial - get post and cookies
POST Method – (showdata.php)

PHP7 – Cookies

A cookie is a small piece of a data file that used to identifier user activities, such as login user details, visited pages, previously entered information, and etc. Cookies are stored on a users computer.

PHP – Setting a Cookie

This function must add before the <html> tag.

setcookie(name, value, expire, path, domain, secure, httponly);

name (required) – Name of the cookie.
value (optional) – Value of the cookie.
expire (optional) – Time the cookie expires. (Ex: time() + 3600)
path (optional) – The path on the server.
domain (optional) – The domain/subdomain.
secure (optional) – The cookie should only be transferred over an HTTPS (HyperText Transfer Protocole Secure) connection.
httponly (optional) – This will help to reduce identity theft through XSS attacks. If set to true the cookie will not be accessible by scripting languages, such as JavaScript.

PHP – Getting a Cookie

echo $_COOKIE["Cookie Name"];

Example:

PHP File (index.php)

<?php

//cookie will expire when the browser close
setcookie("UserID", "012485");

//cookie will expire in 10 Minute
setcookie("UserID", "012485", time() + 600);

?> 

<html>
<body>
<h2> Get Cookie </h2>
<?php

if(!isset($_COOKIE["UserID"])){
    echo "Cookie is not set!";
}else{
    $UserID = $_COOKIE["UserID"];
    echo "Cookie is set!<br>";
    echo "Value is: " . $_COOKIE["UserID"];
}

?>
</body>
</html>
php7 tutorial - get post and cookies
Cookie – (index.php)
php7 tutorial - get post and cookies
Cookie – (index.php)
php7 tutorial - get post and cookies
Cookie

PHP – Updating Cookie

<?php 
setcookie(name, value); //Creating a Cookie

setcookie(name, new value); //Updating a Cookie
?>

Example:

PHP File (index.php)

<?php
$cookie_name = "UserID";
$cookie_value = 123596;
setcookie($cookie_name, $cookie_value); //Value = 123596

$new_cookie_value = 234607;
setcookie($cookie_name, $new_cookie_value); //Value = 234607
?>

<html>
<body>

</body>
</html>

PHP – Deleting a Cookie

unset($_COOKIE["Cookie Name"]);
OR
setcookie("Cookie Name","Value", time()-1); 

Example:

PHP File (index.php)

<?php

setcookie("UserID", "012485", time() + 600);
unset($_COOKIE["UserID"]);

?> 

<html>
<body>
<h2> Get Cookie </h2>
<?php

if(!isset($_COOKIE["UserID"])){
    echo "Cookie is not set!";
}else{
    $UserID = $_COOKIE["UserID"];
    echo "Cookie is set!<br>";
    echo "Value is: " . $_COOKIE["UserID"];
}

?>
</body>
</html>

For more information, Visit:  https://www.php.net/docs.php

Sources: 
https://pixabay.com (Images)

Hi Guys,

Hope this post (PHP7 Tutorial | Part 2 – GET/POST and Cookies) will help you to learn something. If you enjoy my work, please Share my posts with your friends and anyone who might be interested in programming. Don’t forget to Comment and, Subscribe.

What do you think about PHP
See you in the comment section.

Like ctechf Facebook fan page : https://www.facebook.com/ctechf

Also Read :

How to be a Programmer

Leave a Reply

44 Shares
Share
Tweet
Share
Pin
Share