I set session message in my class file like $_SESSION['op_status'] = 'MY MESSAGE HERE'; then redirect the page header("location:news.php"); Page redirects to news.php but session message is not displaying.
config.php:
<?php
session_start();
// DATABASE CONFIURATION GOES HERE
?>
news.php:
<?php
require_once 'config.php';
require_once 'classes/class.news.php';
$objNews = new News();
if(isset($_POST['submit']) && $_POST['submit'] == 'add')
$objNews->add();
?>
<span class='text-warning'><?php echo $_session['op_status']; ?></span>
class.news.php:
public function add() {
if(){
// SOME CODE GOES HERE
} else {
$_SESSION['op_status'] == 'Already Exist';
}
}
You need to use exit() just after header().
$_SESSION['msg']="Hello";
header("Location: account.php");
exit();
then in account.php use
echo $_SESSION['msg'];
Make sure session_start(); is on every page where you want to show/create session messages.
Instead of using this way, I prefer to use below function. (to distinguish if it was error message or success as in Bootstrap HTML markup)
function _redirect($url=NULL, $message=NULL, $message_type=NULL){
$_SESSION['sess_flash_message']= array();
if($message){
switch($message_type){
case 'success': $_SESSION['sess_flash_message'][] = '<div class="alert alert-success"><button data-dismiss="alert" class="close" type="button">×</button>'.$message.'</div>';break;
case 'error': $_SESSION['sess_flash_message'][] = '<div class="alert alert-error"><button data-dismiss="alert" class="close" type="button">×</button>'.$message.'</div>';break;
case 'notice': $_SESSION['sess_flash_message'][] = '<div class="alert alert-info"><button data-dismiss="alert" class="close" type="button">×</button>'.$message.'</div>';break;
case 'warning': $_SESSION['sess_flash_message'][] = '<div class="alert alert-block"><button data-dismiss="alert" class="close" type="button">×</button>'.$message.'</div>';break;
default: $_SESSION['sess_flash_message'][] = $message;
}
}
if($url) {
header("Location: ".$url);
} else {
header("Location: ".$_SERVER['HTTP_REFERER']);
}
exit();
ob_flush();
}
and call _redirect('login.php','You need to login first!','warning');
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With