1<?php
2 // Start the session
3 session_start();
4
5 // Set session variables
6 $_SESSION["color"]= "blue";
7 $_SESSION["animal"]= "dog";
8 echo "The session variable are set up.";
9?>
1/*Sessions are stored on the server
2Sessions are a way to carry data across multiple pages.
3Typically if we set a variable on one page, it wouldn't be available
4on the next page. This is where Sessions come in. Unlike cookies session
5data is not stored on the user's computer. It is stored on the server.
6In order to use session variables you have to start a session.
7Every page, that you want to use that data in, you have to use
8session_start.
9
10If you want to unset one of these sessions you can use session_unset
11
12youcan destry the session with session_destroy
13*/
14
15<?php if(isset($_POST['submit']))
16{ session_start(); // that will start the session
17$_SESSION['name'] = htmlentities($_POST['name']);
18$_SESSION['email'] = htmlentities($_POST['email']);
19header('Location: page2.php'); } ?>
20<!DOCTYPE html>
21<html>
22<head>
23<title>PHP Sessions</title>
24</head>
25<body>
26<form method="POST" action="<?php echo $server['PHP_SELF'];?>">
27<input type="text" name="name" placeholder="Enter Name">
28<br>
29<input type="text" name="email" placeholder="Enter Email">
30<br>
31<input type="submit" name="submit" value="submit">
32</form>
33</body>
34</html>
1<?php
2// start session before any HTML
3session_start();
4
5// create some variales in $_SESSION
6$_SESSION['name'] = 'myname';
7$_SESSION['surname'] = 'mysurname';
8$_SESSION['age'] = 24;
9?>
10
11<!DOCTYPE html>
12<html>
13 <head>
14 <meta charset="utf-8" />
15 <title>My page</title>
16 </head>
17 <body>
18 <p>
19 Hey <?php echo $_SESSION['name']; ?> !<br />
20 How are you today ?
21 </p>
22