Friday, May 13, 2016

PHP Tutorial 6

Using the Header to Force Page Redirect 

<?php ob_start() ?>

<h1> My Page </h1>
This is my page.

<?php
// ob_start
$redirect_page = 'http://www.google.com';
$redirect = false;
if($redirect == true){
header('Location: '.$redirect_page);
}

ob_end_flush();

?>

Setting PHP Sessions
set.php
<?php
// Session
session_start();
$_SESSION['username']='alex';
?>

view.php
<?php
// Session
session_start();
if(isset($_SESSION['username'])){
echo 'Welcome, '.$_SESSION['username'];
} else {
echo 'Please log in!';
}

?>

Unsetting PHP Sessions
unset.php
<?php
// Session
session_start();

unset($_SESSION['username']);

// session_destroy();
?>

Creating Cookies with PHP
set.php
<?php
// Cookie
setcookie('username', 'alex', 1);
?>

view.php
<?php
echo $_COOKIE['username'];
?>


No comments:

Post a Comment