Hello,

I have created many section like update delete etc on the webpage i stored a message in the session variable so when users updates deletes etc on the page a message comes up logo updated sucessfully message posted successfully etc etc so that message is stored in session variaable once things are done and on that page a message is then retrieved so that message will be stored until unless we logged out correct as then session is destroyed so I placed null once the message is viewed once so on next refresh the message is then destroyed but when I placed null the message is not coming up I hope i hade it clear and what help I am looking for

Here is my update query code

function update_banner($connect, $id) {

        $bannername = $_FILES["bgimg"]["name"];
        $imgtype    = $_FILES["bgimg"]["type"];
        $imgtemp    = $_FILES["bgimg"]["tmp_name"];

        $path = "./img/banners/".$bannername;

        $heading = $_POST["bheading"];
        $sptext  = mysqli_real_escape_string($connect, $_POST["sptext"]);
        $btnlink = $_POST["btnlink"];
        $btntext = $_POST["btntext"];
        $section = $_POST["section"];

        move_uploaded_file($imgtemp, $path);
        if(!empty($bannername)) {
            $query_insert = "UPDATE banners SET banner_heading='$heading', spantext='$sptext', button='$btntext', buttonlink='$btnlink', bgimg='$bannername', section='$section' WHERE bid='$id'";
            $confirm      = mysqli_query($connect, $query_insert);
        } else {
            $query_insert = "UPDATE banners SET banner_heading='$heading', spantext='$sptext', button='$btntext', buttonlink='$btnlink', section='$section' WHERE bid='$id'";
            $confirm      = mysqli_query($connect, $query_insert);
        }

        if($confirm) {
            $_SESSION["message"] = "Banner updated successfully";   
            header("Location: optiontheme.php?id=".$_SESSION["id"]);    
        } else {        
            $_SESSION["message"] = "There was some errors please re-enter your information";
            header("Location: optiontheme.php?id=".$_SESSION["id"]);
        }
    }

and here is my session function of message

session_start();

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

            $_SESSION['message'] = null;

            return $output;
        }
    }

then the message is called like this

echo message();

Member Avatar for diafol

I have created many section like update delete etc on the webpage i stored a message in the session variable so when users updates deletes etc on the page a message comes up logo updated sucessfully message posted successfully etc etc so that message is stored in session variaable once things are done and on that page a message is then retrieved so that message will be stored until unless we logged out correct as then session is destroyed so I placed null once the message is viewed once so on next refresh the message is then destroyed but when I placed null the message is not coming up I hope i hade it clear and what help I am looking for

Jeez - take a breath did you? Ever heard of punctuation? I was gasping for oxygen after reading that!

From what you're saying you set the message variable to null and then when you call the message variable is displays nothing (null). So what did you expect to happen?

Okay sorry i was really worried while i was writting though :)

Okay let me explay you

function message() {
        if(isset($_SESSION['message'])) {// if message is set in the session variable then process
            $output  = "<div class='msg'>";
            $output .= $_SESSION['message']; // retreives the message right
            $output .= "</div>";
            $_SESSION['message'] = null;
            return $output;
        }
    }

this null which i placed $_SESSION['message'] = null; is because you know if i had not placed null then whatt happens ?? the users refresh or comes on the page again after a while or a second the message would be displayed stil as session is not destroyed yet becasue the user is alreaddy logged in right?

So this is the main reason why i placed it to null means function runs message display then it becomes null should display once make sense

Please give me a better solutions

Member Avatar for diafol

I'd do

unset($_SESSION['message']);

instead of setting it to null. Is that it?

no that didn't work eighter what happens is when i remove this and I make my function code like this

<?php
    session_start();

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

            return $output;
        }
    }
?>

it works fine but when i refresh my page the message stay's it din't removed should be displayed once though but when i insert unset then it did not displays message once either

<?php
    session_start();

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

            unset($_SESSION['message']);

            return $output;
        }
    }
?>

One thing I wanna ask: are you defining all the functions in a class? or just a normal php and inplement the functions.
If it is second choice, then the problem might be that when you calling message() function, the session may not be started as you put the session_start() outside of the function.
If I am in the case, what I will try is moving session_start() into the message():

<?php
    function message() {
        // check if $_SESSION is not array or not set
        if(!isset($_SESSION) && !is_array($_SESSION)){
            session_start();
        }
        if(isset($_SESSION['message'])) {
            $output  = "<div class='msg'>";
            $output .= $_SESSION['message']; 
            $output .= "</div>";
            unset($_SESSION['message']);
            return $output;
        }
    }
?>

yes i am using the second option but the code you provided didn't worked either

this function is created in session.php in a seperate file

it works fine but when i refresh my page the message stay

How do you refresh the page? If you hit F5 the browser may load the cached page, instead of requesting it again.

Member Avatar for diafol

it works fine but when i refresh my page the message stay's it din't removed should be displayed once though but when i insert unset then it did not displays message once either

Sorry UK, I don't follow this at all. Try breaking the sentence down into smaller ones. Perhaps that will help.

I'm assuming that you're getting the message again on refresh even though it should have been unset. is that right?

okay

Here I would break my sentence to define you each my thoughts or bugs what to say.

when i remove unset($_SESSION["message"]; the message displays in a right order or in the same way in which I was looking.

Okay now I go back to the code an wrote unset($_SESSION["message"]; inside the message function and save now when I updtae the reecord the message should be displayed but it didn't happend though

Make sense I hope I made it clear

Please let me know so I can eleborate my words in short we can say that the session does not unset by it self as we have saved the message in session variaable so there must be a way where we can display the message once

Member Avatar for diafol
session_start();

function get_alert()
{
    $html = '<div id="msg">';
    $html .= $_SESSION['msg'];
    $html .= " and some extra nonsense"; //just for testing
    $html .= '</div>';
    unset($_SESSION['msg']);
    return $html;
}

//TEST CODE

if(isset($_SESSION['msg']))
{
    echo "SESSION MSG EXISTS:";
    echo get_alert();
}else{
    echo "SESSION MSG DOES NOT EXIST";
    if (session_status() == PHP_SESSION_NONE) {
        echo "<br /> SESSION ITSELF DOES NOT EXIST";
    }
}

Try that little bit. See what happpens.

hello got this message

SESSION MSG DOES NOT EXIST

I posted the code as

functions.php

require_once("session.php");

    function get_alert()
        {
            $html = '<div id="msg">';
            $html .= $_SESSION['msg'];
            $html .= " and some extra nonsense"; //just for testing
            $html .= '</div>';
            unset($_SESSION['msg']);
            return $html;
        }
        //TEST CODE
        if(isset($_SESSION['msg']))
        {
            echo "SESSION MSG EXISTS:";
            echo get_alert();
        }else{
            echo "SESSION MSG DOES NOT EXIST";
            if (session_status() == PHP_SESSION_NONE) {
                echo "<br /> SESSION ITSELF DOES NOT EXIST";
            }
        }

function update_banner($connect, $id) {

        $bannername = $_FILES["bgimg"]["name"];
        $imgtype    = $_FILES["bgimg"]["type"];
        $imgtemp    = $_FILES["bgimg"]["tmp_name"];

        $path = "./img/banners/".$bannername;

        $heading = $_POST["bheading"];
        $sptext  = mysqli_real_escape_string($connect, $_POST["sptext"]);
        $btnlink = $_POST["btnlink"];
        $btntext = $_POST["btntext"];
        $section = $_POST["section"];

        move_uploaded_file($imgtemp, $path);
        if(!empty($bannername)) {
            $query_insert = "UPDATE banners SET banner_heading='$heading', spantext='$sptext', button='$btntext', buttonlink='$btnlink', bgimg='$bannername', section='$section' WHERE bid='$id'";
            $confirm      = mysqli_query($connect, $query_insert);
        } else {
            $query_insert = "UPDATE banners SET banner_heading='$heading', spantext='$sptext', button='$btntext', buttonlink='$btnlink', section='$section' WHERE bid='$id'";
            $confirm      = mysqli_query($connect, $query_insert);
        }

        if($confirm) {
            $_SESSION["msg"] = "Banner updated successfully";   
            header("Location: optiontheme.php?id=".$_SESSION["id"]);    
        } else {        
            $_SESSION["msg"] = "There was some errors please re-enter your information";
            header("Location: optiontheme.php?id=".$_SESSION["id"]);
        }
    }

themeoption.php

echo message();


                    if(isset($_SESSION['msg']))
        {
            echo "SESSION MSG EXISTS:";
            echo get_alert();
        }else{
            echo "SESSION MSG DOES NOT EXIST";
            if (session_status() == PHP_SESSION_NONE) {
                echo "<br /> SESSION ITSELF DOES NOT EXIST";
            }
        }

session.php

<?php
    session_start();

    function message() {

        unset($_SESSION['msg']);

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

            return $output;
        }
    }
?>
Member Avatar for diafol

Ok so bow you know session variable does not exist. You do have session_start at the top of every main page?

Yes i simply called functions on my header as session _start is set on the session page

so what happens is session_start on session.php page
session.php is called on function.php page
function are called on themeoption.php file

is this the right order or corect way ?

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

    if(isset($_POST["bannerup"])) {
        $bid = $_POST["bid"];
        echo update_banner($connection, $bid);
    }


    require_once("layout/header.php");
?>
Member Avatar for diafol

This is what you posted?

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

You unset the message before checking if it exists (isset). Just place the unset line before the return line.

Hello,

session.php

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

stilll not working it up what should i do now

okay now I updated and got a new message check this out

error mesage

SESSION MSG EXISTS:
Notice: Undefined index: message in C:\wamp\www\smug\admin\includes\functions.php on line 20
and some extra nonsense

functions.php

function message() {

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

            unset($_SESSION['message']);

            return $output;
        }
    }

    function get_alert()
        {
            $html = '<div id="msg">';
            $html .= $_SESSION['message'];
            $html .= " and some extra nonsense"; //just for testing
            $html .= '</div>';
            unset($_SESSION['message']);
            return $html;
        }
        //TEST CODE
        if(isset($_SESSION['message']))
        {
            echo "SESSION MSG EXISTS:";
            echo get_alert();
        }else{
            echo "SESSION MSG DOES NOT EXIST";
            if (session_status() == PHP_SESSION_NONE) {
                echo "<br /> SESSION ITSELF DOES NOT EXIST";
            }
        }

and one thing more on every refresh the message still exist its does not unset the message on refresh

updated message code

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

            return $output;
        }
    }
Member Avatar for diafol

WHy have you got message() AND get_alert() ? You only need one of them surely?

this is the page where i am calling the message though

if(isset($_SESSION['message']))
    {
        echo get_alert();
    }else{
        echo "SESSION MSG DOES NOT EXIST";
        if (session_status() == PHP_SESSION_NONE) {
            echo "<br /> SESSION ITSELF DOES NOT EXIST";
        }
    }

function.php

    function get_alert {
        {
            $html = '<div id="msg">';
            $html .= $_SESSION['message'];
            $html .= " and some extra nonsense"; //just for testing
            $html .= '</div>';
            unset($_SESSION['message']);
            return $html;
        }
        //TEST CODE
        if(isset($_SESSION['message']))
        {
            echo "SESSION MSG EXISTS:";
            echo message();
        }else{
            echo "SESSION MSG DOES NOT EXIST";
            if (session_status() == PHP_SESSION_NONE) {
                echo "<br /> SESSION ITSELF DOES NOT EXIST";
            }
        }
    }

once i made any updated this message stays
SESSION MSG DOES NOT EXIST

Member Avatar for diafol

Seem to be going around in circles here. Can you show all the relevant code again - having deleted all the unnecessary stuff like the message() function.

Okay here you go

functions.php

<?php
        session_start();

        function get_alert() {
            $html = '<div id="msg">';
            $html .= $_SESSION['message'];
            $html .= " and some extra nonsense"; //just for testing
            $html .= '</div>';
            unset($_SESSION['message']);
            return $html;
        //TEST CODE
        if(isset($_SESSION['message']))
        {
            echo "SESSION MSG EXISTS:";
            echo message();
        }else{
            echo "SESSION MSG DOES NOT EXIST";
            if (session_status() == PHP_SESSION_NONE) {
                echo "<br /> SESSION ITSELF DOES NOT EXIST";
            }
        }
    }

    function add_banner($connect) {

    $logoname = $_FILES["bgimg"]["name"];
    $imgtype  = $_FILES["bgimg"]["type"];
    $imgtemp  = $_FILES["bgimg"]["tmp_name"];

    echo $path = "./img/banners/".$logoname;

    $heading = $_POST["heading"];
    $sptext  = mysqli_real_escape_string($connect, $_POST["sptext"]);
    $btnlink = $_POST["btnlink"];
    $btntext = $_POST["btntext"];
    $section = $_POST["section"];

    move_uploaded_file($imgtemp, $path);

    $query_insert = "INSERT INTO banners(banner_heading, spantext, button, buttonlink, bgimg, section) VALUES('$heading', '$sptext', '$btntext', '$btnlink', '$logoname', '$section')";
    $confirm      = mysqli_query($connect, $query_insert);

    if($confirm) {
        $_SESSION["message"] = "Logo updated successfully"; 
        header("Location: optiontheme.php?id=".$_SESSION["id"]);    
    } else {        
        $_SESSION["message"] = "There was some errors please re-enter your information";
        header("Location: optiontheme.php?id=".$_SESSION["id"]);
    }
}


    function update_banner($connect, $id) {

        $bannername = $_FILES["bgimg"]["name"];
        $imgtype    = $_FILES["bgimg"]["type"];
        $imgtemp    = $_FILES["bgimg"]["tmp_name"];

        $path = "./img/banners/".$bannername;

        $heading = $_POST["bheading"];
        $sptext  = mysqli_real_escape_string($connect, $_POST["sptext"]);
        $btnlink = $_POST["btnlink"];
        $btntext = $_POST["btntext"];
        $section = $_POST["section"];

        move_uploaded_file($imgtemp, $path);
        if(!empty($bannername)) {
            $query_insert = "UPDATE banners SET banner_heading='$heading', spantext='$sptext', button='$btntext', buttonlink='$btnlink', bgimg='$bannername', section='$section' WHERE bid='$id'";
            $confirm      = mysqli_query($connect, $query_insert);
        } else {
            $query_insert = "UPDATE banners SET banner_heading='$heading', spantext='$sptext', button='$btntext', buttonlink='$btnlink', section='$section' WHERE bid='$id'";
            $confirm      = mysqli_query($connect, $query_insert);
        }

        if($confirm) {
            $_SESSION["msg"] = "Banner updated successfully";   
            header("Location: optiontheme.php?id=".$_SESSION["id"]);    
        } else {        
            $_SESSION["msg"] = "There was some errors please re-enter your information";
            header("Location: optiontheme.php?id=".$_SESSION["id"]);
        }
    }

?>

themeoption.php

    require_once("includes/connection.php");
    require_once("includes/functions.php");

    if(isset($_POST["banner"])) {
        echo add_banner($connection);
    }   

    if(isset($_POST["bannerup"])) {
        $bid = $_POST["bid"];
        echo update_banner($connection, $bid);
    }
    //------------------------------------------------------
    // Placed this code at the top of the page


    if(isset($_SESSION['message']))
        {
            echo get_alert();
        }else{
            echo "SESSION MSG DOES NOT EXIST";
            if (session_status() == PHP_SESSION_NONE) {
                echo "<br /> SESSION ITSELF DOES NOT EXIST";
            }
        }
Member Avatar for diafol

In get_alert() - the test code willnever run because it follows the return statement

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.