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?
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
Basically, you can do this using two step action, using either sessions or cookies
Show the div
Check if a parameter is previously set
If not show the div
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.
i think you need to set cookie for that in php and check if cookie value is set then don't show pop up message
I think you can open browser easily, but your question is it should be open only first time even user refresh or reload page it should not display again.
Every time page will load it will check if they have cookie?
If they have then they do nothing.
If they do not have, then display poup and set cookie.
this is you may have to do (use the cookie to store that data)
Related
Please help me out to clear login session.
For example
If I am a user of a particular & I want to check my updates. So I used my user id & password to login to the particular page.
After checking my updates I logged out from the page. After logging out, I used back button in the browser to go back to previous session.
Automatically enters into my page without giving any login details. To prevent from entering into page without any login details the session should be cleared. So help me out to clear the session.
This is a client side problem. If you press back button in browser, the browser loads the previously stored cached page.
Better way is, You should write a javascript function in login page which invokes logout.jsp whenever loaded first time or through back button.
You need to set the cache control header on your pages to ensure they are not cached. Since Have a look at the following question which goes into detail about how to set the cache header correctly in different languages - How to control web page caching, across all browsers?
To clear session in php, you can call a script logout.php containing the following :
<?php
session_start();
$_SESSION = array();
// PHPSESSID is default name, but you may have a custom.
setcookie("PHPSESSID", '', time() - 1, '/');
}
See a more complete example at
http://fr.php.net/session_destroy
I have a page tab app that is a contest with a like gate.
A user likes a page, provides their email address and hits submit to enter the contest.
After they enter, I set a session variable $_SESSION['entered'] = '1'; or something similar
Once they enter, I show more information on the page(html) and remove the email form.
PROBLEM: If the users stays on the page, refreshes it a million times (in the same browser window), the session variable stays and I'm able to tell that the user has entered and display the correct info.
If the user closes the window, comes back later in a new window, etc. I lose the session information and the user is presented with the enter form again (although they've already entered the contest)
How do I sustain the session for the user in the page app if they leave and come back?
You could use a cookie instead. Sessions will close out on exiting the browser, but cookies will last until the expiration date is reached on them.
You could also set it up that they have to log in, and in their user, you set whether they have entered the contest or not and adjust the page accordingly. (just an idea...)
Sessions are destroyed by the browser when closed - nothing code wise will change how browsers handle that. You can try a cookie instead, or try to save the session to their IP/useragent (wouldn't recommend for security though) and reestablish it when they come back.
As written by others Sessions are destroyed if a user closes the browser. i won`t use cookies on your side either. I suggest you to get the users uid and check your database if the user has already entered the form and display the correct page after you have this info.
This might be a more secure and consistent way than sessions or cookies.
Greets
If I have for example 7 open tabs with user personal profile i browser, after session is going down user sees the alert confirmation does he wan't to continue his session or not, if not, session destroes and all 7 tabs with his personal profile should be loaded end php redirect them to login form.
here is the question, how can I determine that the session were destroed and we should reload tabs? Ajax is not good solution coz it's make a lot of queries to server
I think AJAX would be the solution, there's no need to make a lot of queries. Just use a javascript callback function which is executed once each 5 minuts and checks if user has chosen to not continue his session. If yes, then redirect...
If you do not wish to use AJAX, which is the only available solution I know of for dynamic refresh/closing capabilities, you will have to check if the session exists each time the page is loaded to determine if the page should be reloaded or closed. You can do this by saving the session id in a cookie and comparing it each time the page is loaded. This will tell you if the session has ended and can allow you to reload it if I recall correctly.
I have a moveable and closable jquery pop-up notification box. It works well aside from the fact that it pops up every time the page is loaded. What method would I need to implement to allow a user to permanently (or at least semi permanently) close the pop-up box?
If I need to use cookies how would I tie a cookie to a specific action like closing the div?
yes you can use cookies. when user click on the close button you can write it to the cookies. and when page load, if cookie is not available display the popup
You could simply use client side cookies: http://www.w3schools.com/js/js_cookies.asp
If the user doesn't have a cookie present, display box;
If the user has the cookie present and cookie specifies the user has already closed the box, keep it closed; etc..
It's simple, and doesn't put any extra weight on the server, you can also set a large expiry date if you want the popup not to show on theusers next visit for example.
Although this does depend on what it's for as sessions may also be another way of handling this. (Sessions may mean that if the user comes back the next day for instance, the popup will show again depending on how it's set up)
session would be another candidate and its secure than cookies. And you don't have to do anything else than setting a variable on loading popup on first time and check afterwards if that variable is set or not.
Yes you can use cookies to do the trick, basically you're checking to see if the variable in the cookie is set, if not update the variable:
http://www.w3schools.com/js/js_cookies.asp
Don't forget that on close, you should update the cookie variable.
For example, code such as the following doesn't seem to work, even if the cookie is actually set, until I refresh the page.
setcookie("cookies","1", time()+ 86400,"/" );
if (!isset($_COOKIE["cookies"])) {
$cookies = "foobar";
}
Im trying to write an accurate "unique user" counter on my site, which works based on checking to see if a user has a cookie set on the computer, if they don't, it does +1, and sets the cookie. The problem here is that, the users without cookies will just +1 on every page load, so naturally I only want to run this code for those who have cookies enabled.
The cookie checker above however will always return $cookies = "foobar" on the first page load, regardless of users having cookies on, or off. So if a user just views 1 page of the site, their visit won't be registered by the counter.
You cannot immediately check for its existence of cookies by checking the set value, because cookies are not sent to the browser until the next round trip to the client.
One thing you could do is set the cookie on the page, and then check it on an image script.
There are other workarounds.
The idea that comes to my mind is doing a javascriptcheck after the page is loaded, then using ajax to write the counter to the database or whereever you wish to store it. PHP is serverside while the cookie is stored clientside so doing the check like you're trying to is going to be a problem.