Hello,

I have created many functions and I am trying to show messages ie when user successfully registered invalid username password invalid this and that I tried to do so using the session_start concept please let me know if this is a good approach or should i do another way if possible as this is not working.

Here is how I created.

my structure path is :
mainfolder>includes>functions.php (Where all of my fucntions are currently stored in)
mainfolder>index.php where I want to display the message

My code functions.php

session_start();

function message() {
    if(isset($_SESSION['message'])) {
        $output  = "<div class='msg'>";
        $output .= $_SESSION['message']; 
        $output .= "</div>";

        unset($_SESSION['message']);

        return $output;
    }
}

function adduser($connect) {
    (all my other code)
        if($insert) {
            $_SESSION["msg"] = "You have successfully registered";
            header("Location: login.php");
        } else {
            $_SESSION["msg"] = "There were some errors";
            header("Location: signup.php");
    }
}

function update_user($connect, $id) {
    (All my other code)
    if($insert) {
        $_SESSION["msg"] = "You information have been successfully updated";
        header("Location: user.php?uid=".$_SESSION["uid"]);
    } else {
        $_SESSION["msg"] = "There were some errors";
        header("Location: user.php?uid=".$_SESSION["uid"]);
    }
}

and on index.php I simply do this <?php echo message(); ?> But the message is not showing up though. Now I have noticed one thing when I remove unset($_SESSION['message']); from my message function then message will be shown up but never ever removes as it's stored in session and will be remoevd once session is destroyed so what should I do now?

Thank You

Hi,

Sorry but on update and add functions you're setting $_SESSION['msg'], on message() instead you're dealing with $_SESSION['message'], is that a mistype?

I built a simple test and works fine:

<?php

    session_start();

    function message()
    {
        if(isset($_SESSION['msg']))
        {
            $output = "<div class=\"msg\">{$_SESSION['msg']}</div>";
            unset($_SESSION['msg']);
            return $output;
        }
    }

    function insert()
    {
        $r = mt_rand(1, 2);

        if($r > 1)
            $_SESSION['msg'] = 'Failed @ '. time();

        else
            $_SESSION['msg'] = 'Success @ '. time();
    }

    if($_SERVER['REQUEST_METHOD'] == 'POST')
        insert();

?>
<!DOCTYPE html>
<html>
<head>
    <title>Test</title>
</head>
<body>

    <?php print message(); ?>

    <hr>

    <form method="post">
        <input type="submit">
    </form>

    <hr>

    <a href=".">refresh</a>

</body>
</html>

Sorry it was mistyped while posting the question. and tried your but it is not working though i removed session_start(); from functions.php and placed it on index.php so my head code after refactor is

<?php
    session_start();
    require_once("includes/functions.php");
    require_once("includes/connection.php");

    function message()
    {
        if(isset($_SESSION['msg']))
        {
            $output = "<div class=\"msg\">{$_SESSION['msg']}</div>";
            unset($_SESSION['msg']);
            return $output;
        }
    }
    function insert()
    {
        $r = mt_rand(1, 2);
        if($r > 1)
            $_SESSION['msg'] = 'Failed @ '. time();
        else
            $_SESSION['msg'] = 'Success @ '. time();
    }
    if($_SERVER['REQUEST_METHOD'] == 'POST')
        insert();

    if(isset($_POST["register"])) {
        echo adduser($connection);
    }

    if(isset($_POST["login"])) {
        echo verify($connection);
    }

    require_once("includes/header.php");
?>

My test code (the insert() function) will not work in yours if there's not a POST request (you have to press the submit button of the form at the bottom of the script), my purpose is to verify that, at basic, unset() does not affects the output of the message() function. If you want to test, then you should run my script without including your code, if it works fine then you have to point your attention to something else: at this point it would be helpful to see also the other code in the add and update functions to see if there is something that can disturb the execution. Otherwise just add the time() function to your messages, like in my example, to see if it updates.

The example is live here: http://code.runnable.com/VgvCvR-X77ddKdH6/testing-sessions-unset-for-php

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.