user4422481
user4422481

Reputation:

store error message in session and display on another page

there is a scenario that i have in my hand at present.I have 2 pages index.php and code.php.

the user first goes to index.php page and then is redirected to code.php page and back to index.php page.

in case some error occurs in code.php page, i need to display that error on index.php page when it gets redirected back to index page.

i stored the error message in session like this and then redirected the page bsck to index.php page

$_SESSION["errormsg"]='please try again';
header("Location: index.php");

but if i echo this session message in index.php page it gets displayed evertime when the page gets loaded, however i want only once when the index page comes after the code.php page. can anyone tell how it can be done

Upvotes: 3

Views: 26308

Answers (5)

d.dean
d.dean

Reputation: 11

The question has been answered 3 years ago, but i thought i could add something into the same topic. I hope it is helpful.

If session message in index.php page gets displayed every time you open the page, it means that session variable "errormsg" has not been cleared.

Therefore use unset($_SESSSION['errormsg']); to remove it after display as suggested above.

However when you do this, another problem occurs the error message wont be displayed with this code in code.php:

$_SESSION["errormsg"]='please try again';

header("Location: index.php");

Because when you redirect to the index page, session variable is cleared before message gets displayed.

To solve this:

Avoid redirecting the page more than once.

in code.php use:

$_SESSION["errormsg"]='please try again';

without redirecting to the index.php file.

Upvotes: 1

Abdo-Host
Abdo-Host

Reputation: 4193

//Ensure that a session exists (just in case)
if( !session_id() )
{
    session_start();
}

Function

/**
 * Function to create and display error and success messages
 * @access public
 * @param string session name
 * @param string message
 * @param string display class
 * @return string message
 */
function flash( $name = '', $message = '', $class = 'success fadeout-message' )
{
    //We can only do something if the name isn't empty
    if( !empty( $name ) )
    {
        //No message, create it
        if( !empty( $message ) && empty( $_SESSION[$name] ) )
        {
            if( !empty( $_SESSION[$name] ) )
            {
                unset( $_SESSION[$name] );
            }
            if( !empty( $_SESSION[$name.'_class'] ) )
            {
                unset( $_SESSION[$name.'_class'] );
            }

            $_SESSION[$name] = $message;
            $_SESSION[$name.'_class'] = $class;
        }
        //Message exists, display it
        elseif( !empty( $_SESSION[$name] ) && empty( $message ) )
        {
            $class = !empty( $_SESSION[$name.'_class'] ) ? $_SESSION[$name.'_class'] : 'success';
            echo '<div class="'.$class.'" id="msg-flash">'.$_SESSION[$name].'</div>';
            unset($_SESSION[$name]);
            unset($_SESSION[$name.'_class']);
        }
    }
}


//Set the first flash message with default class
flash( 'example_message', 'This content will show up on example2.php' );

//Set the second flash with an error class
flash( 'example_class', 'This content will show up on example2.php with the error class', 'error' );

Displaying the messages

<?php flash( 'example_message' ); ?>
<?php flash( 'example_class' ); ?>

Upvotes: 0

Pulkit Pithva
Pulkit Pithva

Reputation: 152

I think that, the best way to show error message via session is having separate message file like message.php.

Step 1 : you just have to set the session with your error message in your code.php.

$_SESSION["errormsg"]='please try again';

Step 2 :

  • Now create new file with the name "message.php".

  • Store the session error message into new variable.

$error = $_SESSION["errormsg"];

Step 3 :

then, unset the session or destroy the session.

 // remove all session variables
 session_unset(); 

 // destroy the session 
 session_destroy(); 

Step 4 :

  • Now echo that error variable in formatted div tag so that your error looks good and also eye catchy.

<div id='alert'><div class=' alert alert-block alert-info fade in center'>$error</div></div> 
Step 5 :

  • Now, in you index.php file, have a method to check that if the session variable "errormsg" is set or not.
  • If the session variable is set then call the message.php file right away. it will echo that div in you index.php file .

I think this would give you full answer.

Upvotes: 3

Aadil Keshwani
Aadil Keshwani

Reputation: 1386

You can use session_start() at starting of page as you are assigning session variable value without actually starting the session.

Also you can use the condition

<?php
session_start();
if (isset($_SESSION['variablename']))
{
 //your code
}
?>

Upvotes: 0

Vikas Arora
Vikas Arora

Reputation: 1666

Use a simple condition to check if the particular session is set or not.

if(isset($_SESSION["errormsg"])) {
    $error = $_SESSION["errormsg"];
    session_unset($_SESSION["errormsg"]);
} else {
    $error = "";
}

echo $error;

Upvotes: 3

Related Questions