I want to echo a message on a new page after redirecting. but I only want the message to show once after reloading (redirecting) and on the next reload I want the message gone. is this at all possible? I give you an example:
$_SESSION['message'] = "entry deleted"
header("location: anotherpage.html")
on "anotherpage.php"
echo "$_SESSION['message']" // upon next reload $_SESSION['message'] = "";
echo $_SESSION['message'];
unset($_SESSION['message']);
But I prefer to add a function to display messages, something like:
function setMessage($msg_body, $css_style = 'normalMsg') {
$_SESSION['messages'][$css_style][] = $msg_body;
}
function showMessages() {
if (empty($_SESSION['messages'])) return;
foreach ($_SESSION['messages'] as $css_style=>$messages) {
echo '<div class="'.$css_style.'">';
echo implode($messages,'<br>');
echo '</div>';
}
unset($_SESSION['messages']);
}
You can check, if message is set and valid
if (isset($_SESSION['message']) && $_SESSION['message']) {
echo $_SESSION['message'];
$_SESSION['message'] = false;
}
It should be working as you suggested. (If I got you right).
Simply:
the redirecting page :
$_SESSION['message'] = 'test';
header('Location: anotherpage.php');
on "anotherpage.php" :
echo $_SESSION['message'];
$_SESSION['message'] = ''; // message will being empty on further reloads
Related
First page
<?php
session_start(); // put ahead all html tags and echo commands and print.
$_SESSION["username"] = 'admin';
echo 'see session';
?>
Second page
<?php
if( $_SESSION["username"] == 'admin' ) {
echo 'Hello '. $_SESSION["username"] . ' You are adminstrator on this page';
} else {
echo 'You can not accesss';
}
?>
Question
When I click on the link session then I get :
Undefined variable: _SESSION
I have no idea why.
Add in your second file at the start session_start();
like that:
<?php
session_start();
if( $_SESSION["username"] == 'admin' )
{
echo 'Hello '. $_SESSION["username"] . ' You are adminstrator on this page';
}
else
{
echo 'You can not accesss';
}
?>
You need to put session_start(); at the begin of the second page.
The $_SESSION['isLogin'] default value is 0.
I want to modify the $_SESSION['isLogin'] value to 1
(not in the script)
<?php
session_start();
$_SESSION['isLogin'] == 1 {
echo "Hello";
} else {
echo "You need login before enter this page !";
}
?>
Yes you can modify the value of $_SESSION variable.You can try for
session_start();
$_SESSION['isLogin'] = 1;
if(isset($_SESSION['isLogin'])) {
echo "Hello";
} else {
echo "You need login before enter this page !";
}
Trying to display this data in the diary post page solely based on the Session variable.
The Session variable is already declared in the php/ functions file.
The data foreign key is already linked between the User ID in the accounts table and the diary post table in PHP my admin
I just need an Inner join query of some description to only show the data based on Session.
Functions declared at the top of diary page
<?php
include("php/functions.php");
$userID = $_SESSION["userID"];
?>
The functions file
<?php
if(session_id() == '') {
session_start();
}
if(!isset($_SESSION['myEmail'])){ //if login in session is not set
header("Location: login.php");
}
if (!isset($_SESSION['myEmail'])) {
echo" <a href='login.php'";
}
else {
$myFName = $_SESSION['userFirstName'];
}
Where I need to display the posts based on Session variable
<?php
// index.php
include 'mysql.php';
echo '<h1>My Positive Experience Diary</h1>';
echo "<em>Post 10 Positive Recent Experiences</em><hr/>";
$result = mysql_safe_query('SELECT * FROM posts ORDER BY date DESC');
if(!mysql_num_rows($result)) {
echo 'No posts yet.';
} else {
while($row = mysql_fetch_assoc($result)) {
echo '<h2>'.$row['title'].'</h2>';
$body = substr($row['body'], 0, 300);
echo nl2br($body).'...<br/>';
echo 'Read More | ';
echo '<hr/>';
}
}
echo <<<HTML
+ New Post
HTML;
?>
The mysql.php where the diarypost page is getting its data.
<?php
// mysql.php
function mysql_safe_string($value) {
$value = trim($value);
if(empty($value)) return 'NULL';
elseif(is_numeric($value)) return $value;
else return "'".mysql_real_escape_string($value)."'";
}
function mysql_safe_query($query) {
$args = array_slice(func_get_args(),1);
$args = array_map('mysql_safe_string',$args);
return mysql_query(vsprintf($query,$args));
}
function redirect($uri)
{
if (!headers_sent())
{
header('Location: '.$uri);
exit;
}
else
{
echo '<script type="text/javascript">';
echo 'window.location.href="'.$uri.'";';
echo '</script>';
echo '<noscript>';
echo '<meta http-equiv="refresh" content="0;url='.$uri.'" />';
echo '</noscript>'; exit;
}
}
#mysql_connect('localhost','########','########');
#mysql_select_db('########');
What it looks like posting all the data from the database not based on User ID
enter image description here
this is my code after success enter login and password
<?php session_start();
if(!isset($_SESSION['nik'])){ die("Anda belum login");}
if($_SESSION['level']!="admin"){
echo "<h3>Welcome ".$_SESSION['nik']."</h3>";
echo "panel user";
}
if($_SESSION['level']="admin"){
echo "panel admin";
}
?>
<a href=log.php?op=selesai>Log Out</a>
the out put that i want is :
if login = admin then display "hello admin"
if login = user then display "hello user"
if not login then say "you must login"
any suggestion how to fix that ?
please find below code as per your requirements.
<?php
session_start();
if(!isset($_SESSION['nik'])){
echo "You must login.";
exit;
}
if($_SESSION['level']!="admin"){
echo "<h3>Welcome ".$_SESSION['level']."</h3>";
echo '<a href=log.php?op=selesai>Log Out</a>';
exit;
}
else {
echo "<h3>Welcome ".$_SESSION['level']."</h3>";
echo '<a href=log.php?op=selesai>Log Out</a>';
}
?>
Thanks.
i choose and use #Ghanshyam answer,
thank you to all member at stackoverflow.
this is very helpfull site
Use following statement to set session for username while login-
session_start();
//following statement will create a session store user name.You can use this variable to display particular output.
$_SESSION["uname"] = $_GET['un'];
//in above statement 'un' is text box from which we are accepting username.
After set session variable for user name you can use it on another page-
session_start();
if(isset($_SESSION["uname"]) )
{
if($_SESSION["uname"] == 'admin')
{
echo 'hello admin';
}
else($_SESSION["uname"] == 'uname')
{
echo 'hello user';
}
}
else
{
echo "you must login"
}
The problem is in your operator selection.
if($_SESSION['level']="admin"){
echo "panel admin";
}
should be
if($_SESSION['level'] == "admin"){
echo "panel admin";
}
= is an assignment operator. And you need to use == or ===
I have one problem.I wanted to echo my message ("subject created", "subject creation failed" depending on whether my subject is created or not). The problem is the message is on every page even though setting the $_SESSION["message"] is under if condition . I really don't know where is the problem. I lost probably 2 hours on this...
All includes and requires are included...
This is on my proceeding page:
if(isset($_POST["submit"])) {
$menu_name = mysql_prep($_POST["menu_name"]);
$position = (int) $_POST["position"];
$visible = (int) $_POST["visible"];
$query = "INSERT INTO subjects(";
$query .= " menu_name, position, visible ";
$query .= ") VALUES ('{$menu_name}', '{$position}', '{$visible}')";
$result = mysqli_query($connection,$query);
if($result) {
//success //
$_SESSION["message"] = "Subject created";
redirect_to("manage_content.php");
} else {
$_SESSION["message"] = "Subject creation failed";
redirect_to("create_new_content.php");
}
} else {
redirect_to("create_new_content.php");
}
my message function is:
session_start();
function message() {
if (isset($_SESSION["message"])){
$output = $_SESSION["message"];
return $output;
}
}
and after all Im echoing on my manage_content.php and create_new_content.php
<?php echo message(); ?>
You should clear the session when its not needed any more.
E.g.
unset($_SESSION['message']);
Try to clear your $_SESSION message and check if is not empty
function message() {
if (isset($_SESSION["message"]) && !empty($_SESSION["message"])){
$output = $_SESSION["message"];
$_SESSION["message"] = '';
return $output;
}
}
if you show your message only one time, you need to clear the $_SESSION["message"] before return
session_start();
function message() {
if (isset($_SESSION["message"])){
$output = $_SESSION["message"];
// clear the session message
$_SESSION["message"] = null;
// remove message index from $_SESSION
unset($_SESSION["message"]);
return $output;
}
}