1<?php
2 // Start the session
3 session_start();
4?>
5<!DOCTYPE html>
6<html>
7 <body>
8 <?php
9 // Set session variables
10 $_SESSION["color"]= "blue";
11 $_SESSION["animal"]= "dog";
12 echo "The session variable are set up.";
13 ?>
14 </body>
15</html>
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
2<?php
3// This sends a persistent cookie that lasts a day.
4session_start([
5 'cookie_lifetime' => 86400,
6]);
7?>
8
9