March 13, 2012

displaying a message as pop up when my site is open

Question by user1263260

I have a web php web site. I want to show a message on the pop up div when a user browse my site (home page). I need to show the message only the first time with in a browser.
Does anyone know?

Answer by AD7six

There is no php in your question, it is only js and cookies.

The logic you want is:

  • Does cookie <name> exist?
  • yes
    • The user has been to your page/site before
    • do nothing
  • no
    • The user is new
    • write cookie <name> with any value
    • run your “display message” function

Answer by Starx

Basically, you can do this using two step action, using either sessions or cookies

  1. Show the div
    • Check if a parameter is previously set
    • If not show the div
  2. Then set a parameter to confirm it is never again, till the browser is closed

Since other answers are focused on cookies, I will give you an example of session.

session_start();
if(!isset($_SESSION['boxshowed']) || !$_SESSION['boxshowed'])) {
   echo "<div>to show</div>";
   $_SESSION['boxshowed'] = true;
}

Using sessions for this, will show the box again, when the user reopens the site, after completely closing it.

Author: Nabin Nepal (Starx)

Hello, I am Nabin Nepal and you can call me Starx. This is my blog where write about my life and my involvements. I am a Software Developer, A Cyclist and a Realist. I hope you will find my blog interesting. Follow me on Google+

...

Please fill the form - I will response as fast as I can!