Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Redirect on logout and display "You have successfully logged out!"

Tags:

html

php

I have a membership service on my website. Currently when someone logs out they are redirected to logout.php that has this code on it:

<?php

        //check if the login session does no exist
        if(strcmp($_SESSION['uid'],”) == 0){
            //if it doesn't display an error message
            echo "<center>You need to be logged in to log out!</center>";
        }else{
            //if it does continue checking

            //update to set this users online field to the current time
            mysql_query("UPDATE `users` SET `online` = '".date('U')."' WHERE `id` = '".$_SESSION['uid']."'");

            //destroy all sessions canceling the login session
            session_destroy();

            //display success message
            echo "<center>You have successfully logged out!<br><a href = '/review-pratt/index.php' class='icon-button star'>Return Home</button></center>";
        }

        ?>

Instead of having the users be taken to "logout.php" and viewing a boring page that says they logged out. I want them to be redirected to index.php. That part is easy, I know.

I am wanting a notification bar across the top to appear notifying them that they successfully logged out. I have tried to do this before and never got anything to work. Any help or suggestions would be appreciated!

Update

I have changed the logout.php code to:

<?php

        //check if the login session does no exist
        if(strcmp($_SESSION['uid'],”) == 0){
            //if it doesn't display an error message
            echo "<center>You need to be logged in to log out!</center>";
        }else{
            //if it does continue checking

            //update to set this users online field to the current time
            mysql_query("UPDATE `users` SET `online` = '".date('U')."' WHERE `id` = '".$_SESSION['uid']."'");

            //destroy all sessions canceling the login session
            session_destroy();

            //Redirect with success message
            header('Location: /index.php?msg=' . urlencode("You have been successfully logged out!"));
        }

        ?>

and added the following code to my index.php:

    <?php

    if ($_GET['msg'])
{
       echo '<div class="success_message">' . base64_decode(urldecode($_GET['msg'])) . '</div>';
}

?>

And when I log out I receive this error:

Warning: Cannot modify header information - headers already sent by (output started at /home/content/38/10473938/html/review-pratt/business_profiles/logout.php:19) in /home/content/38/10473938/html/review-pratt/business_profiles/logout.php on line 35
like image 244
user2109152 Avatar asked Jan 24 '26 22:01

user2109152


2 Answers

you could do something like this:

header('location: index.php?status=loggedout');

and in your index.php file just look to see if status is not empty, and show a div with the status like this:

<?php 
   if(!empty($_GET['status'])){
          echo '<div>You have been logged out!</div>';
   }
?>

also inside that if statement you can clear user session aswell..

like image 70
Andres Avatar answered Jan 27 '26 12:01

Andres


There are many solutions to this, but almost all of them require logout.php to pass the message, and index.php to have code to display the message.

My preferred method is to pass the message as a URL parameter. Use header to re-direct, use base64_encode to shorten the text in the url, and url_encode to make sure that the URL doesn't get junked up.

//Redirect with success message
header('Location: /index.php?msg=' . urlencode(base64_encode("You have been successfully logged out!")));

Then, on your index.php page

if ($_GET['msg'])
{
       echo '<div class="success_message">' . base64_decode(urldecode($_GET['msg'])) . '</div>';
}

Edit: If your headers have already been sent out (have you echoed out some text on a line above these?), you can use Javascript to do the redirection.

Replace header('Location: ') with this: echo '<meta http-equiv="Refresh" content="0;url=http://example.com/index.php?msg=' . urlencode(base64_encode('You have been successfully logged out!')) . '">';

like image 26
Luke Shaheen Avatar answered Jan 27 '26 11:01

Luke Shaheen