How to Pass Variables in PHP Using Sessions and Cookies (Step by Step)

Write PHP script to demonstrate passing variables using sessions and cookies example with output

In PHP, passing variables between pages is a common requirement in web development. This can be achieved using sessions and cookies. In this practical guide, you will learn how to pass variables using both methods with simple and clear examples.

What is Passing Variables in PHP?

Passing variables in PHP means transferring data from one page to another using techniques like sessions, cookies, or URL parameters. Sessions store data on the server, while cookies store data in the user's browser.


1. Passing Variables Using PHP Sessions

Sessions allow you to store user data on the server and access it across multiple pages.

Step 1: index.php (Create Session)

<html>
<head>
<title>Session Demo</title>
</head>
<body>

<form method="post" action="retrieve.php">
 Enter Your Name :
 <input type="text" name="fname">
 <input type="submit" name="submit" value="Create Session">
</form>

</body>
</html>

Step 2: retrieve.php (Access Session)

<?php
session_start();

if(isset($_POST['submit'])) {
    $_SESSION['username'] = $_POST['fname'];
    echo "Your name is: " . $_SESSION['username'];
}
?>

Output:

The entered name will be displayed using session data, see example output below


Explanation: Session data is stored on the server and accessed using $_SESSION.


2. Passing Variables Using PHP Cookies

Cookies store user data in the browser and can be accessed across pages.

Write PHP Script to Demonstrate Passing Variables with Cookies

<?php
if(isset($_POST['submit'])) {
    $name = $_POST['fname'];

    // Set cookie (valid for 1 hour)
    setcookie("username", $name, time() + 3600);

    echo "Cookie Created Successfully!";
}
?>

<html>
<body>

<form method="post">
 Enter Your Name:
 <input type="text" name="fname">
 <input type="submit" name="submit" value="Create Cookie">
</form>

</body>
</html>

Access Cookie Value:

<?php
if(isset($_COOKIE['username'])) {
    echo "Your name is: " . $_COOKIE['username'];
} else {
    echo "Cookie not found!";
}
?>

Explanation: Cookies are stored in the browser and accessed using $_COOKIE.


3. PHP Visitor Counter Program

This program counts how many times a page has been visited using a file.

<?php

$handle = fopen("counter.txt", "r");

if(!$handle) {
    echo "Could not open file";
} else {
    $counter = (int) fread($handle, 20);
    fclose($handle);

    $counter++;

    echo "You are visitor number " . $counter;

    $handle = fopen("counter.txt", "w");
    fwrite($handle, $counter);
    fclose($handle);
}
?>
Output:

Sessions vs Cookies (Quick Difference)

  • Sessions: Stored on server, more secure
  • Cookies: Stored in browser, faster but less secure

Conclusion

In this tutorial, you learned how to pass variables in PHP using sessions and cookies. Sessions are ideal for secure data handling, while cookies are useful for storing small user preferences. Understanding both concepts is essential for building dynamic PHP applications.


Frequently Asked Questions (FAQs)

How to pass variables using cookies in PHP?

To pass variables using cookies in PHP, use the setcookie() function to store data in the browser. You can then access it on another page using the $_COOKIE superglobal variable.

What is the difference between sessions and cookies in PHP?

Sessions store data on the server, making them more secure, while cookies store data in the user's browser and are faster but less secure.

How do you retrieve cookie values in PHP?

You can retrieve cookie values using $_COOKIE['cookie_name']. Always check if the cookie is set using isset() before accessing it.

Why is my PHP cookie not working?

Cookies may not work if output is sent before calling setcookie(). Ensure cookies are set before any HTML output and check browser settings.

Can cookies be used to store sensitive data?

No, cookies are stored in the browser and can be accessed or modified by users. Sensitive data should be stored using sessions instead.


Related Articles

Comments

Post a Comment