PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Tuesday, July 12, 2022

[FIXED] What is the best way to view a successful or a bad message?

 July 12, 2022     get, message, php, session     No comments   

Issue

For a long time, I've been thinking what's best way to display successful or error message. I need to refresh the page and then view the message. If I would use $_GET (for example:/My-Page?status=success), the problem would be that anyone can edit status. It's not a big problem, but I don't know if it's the best idea...

If I use SESSION:

$_SESSION['message'] = "Mail was sent successfully!";
header('Location: '.$_SERVER['REQUEST_URI']);
exit();

//and then:
if (!empty($_SESSION['message'])) {
    echo $_SESSION['message'];
}

I would probably have done what I wanted, but if someone would left and then came back to the page (or just refreshed the page), he will still see a message (and that's not what I want)...

So, to sum up, my question is: What's the best way to save the message to variable to only appear once (after the refresh, the message would disappear) and nobody could edit it? I'm sorry for my English, I hope I wrote it all clearly :-)


Solution

The simplest option is to slightly amend your session based code to clear the session data after displaying it

if (!empty($_SESSION['message'])) {
    echo $_SESSION['message'];
    unset($_SESSION['message']); //will not display again
}


Answered By - Steve
Answer Checked By - Gilberto Lyons (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing