php create session code example
Example 1: php start a session
session_start();
if(!isset($_SESSION["started"]){
$_SESSION["started"] = true;
$_SESSION["attribute_1"] = $value_1;
...
$_SESSION["attribute_n"] = $value_n;
}
Example 2: session initialize php
session_start();
Example 3: set php varibianle session
<?php
session_start();
?>
<!DOCTYPE html>
<html>
<body>
<?php
$_SESSION["color"]= "blue";
$_SESSION["animal"]= "dog";
echo "The session variable are set up.";
?>
</body>
</html>
Example 4: create session in php
<?php
session_start();
$_SESSION["color"]= "blue";
$_SESSION["animal"]= "dog";
echo "The session variable are set up.";
?>
Example 5: $_SESSION php example
<?php
session_start();
echo session_id();
?>
Example 6: php session
<?php if(isset($_POST['submit']))
{ session_start();
$_SESSION['name'] = htmlentities($_POST['name']);
$_SESSION['email'] = htmlentities($_POST['email']);
header('Location: page2.php'); } ?>
<!DOCTYPE html>
<html>
<head>
<title>PHP Sessions</title>
</head>
<body>
<form method="POST" action="<?php echo $server['PHP_SELF'];?>">
<input type="text" name="name" placeholder="Enter Name">
<br>
<input type="text" name="email" placeholder="Enter Email">
<br>
<input type="submit" name="submit" value="submit">
</form>
</body>
</html>