Loading welcome animation only once jquery/php - php

In my site i have a welcome animation (created with jquery) which runs when any one opens my website (www.mysite.com). Then the same user going to anther page in my site for example www.mysite.com/about then click back to home (www.mysite.com) the animation again shows up. How can i restrict this ?
Share some logic like putting a session or settimeout or something like that.

This is how you could do it using jquery cookie plugin:
http://jsfiddle.net/lollero/2nYBV/ ( or http://jsfiddle.net/lollero/2nYBV/show/ )
You might want to change the way cookie expires. That info can be found in the plugin page. The way I did it, it expires after every browser session.
jQuery:
You could just as easily reverse this to hide, if the cookie is set. 1. Set cookie 2. if statement to trigger hide, if cookie is set.
// If "welcome" cookie is not set...
if ( $.cookie('welcome') === null ) {
// Show welcome text.
$('#welcome').show();
// Set the "welcome" cookie, so that the
// welcome text will not be shown after this.
$.cookie('welcome', true );
}
​
CSS:
#welcome {
display: none;
}​
HTML:
<div id="welcome">Welcome</div>​

See the following link for information on how to setup cookies using JQuery:
How do I set/unset cookie with jQuery?
Everytime you load the homepage, you can check if the cookie is set using an if-statement. If the cookie is not set, run the animation.
Within the animation-script; make sure that you set the cookie so that the script won't run again while the cookie is active.

You can set cookie either on server side or either client side
And check whether the cookie is set or not if cookie is set then dont show welcome box otherwise show the welcome box...
In php you can set session cookie using setcookie function please see below link:-
http://php.net/manual/en/function.setcookie.php

Place timer with flag then redirect to main page in your site.
setTimeout(function() {window.location.href = "/NewPage.php";}, 2000);

You can use a session to handle this.
An example:
<?php
$_SESSION['new'] = true;
?>

At first visit main page - define a flag in cookies, and at each visit of page check this flag

Related

How to destroy session in PHP when browser back button is clicked?

I have read many related question here but seems not solve my problem. How to destroy session in PHP when user clicked at the browser back button.
Example, current page is home.php, when back button is clicked, it will go to index.php. So should be session will by destroy.
I trying both options. But still not destroy the session.
First Option (home.php)
<?php
session_start();
if (isset($_SESSION)) {
session_destroy();
}
?>
Second Option (index.php) This is not practical.
<script language="javascript" type="text/javascript">
window.history.forward();
</script>
If you want a reliable way to clear all values of any current session you can use this on the loading of any page where you want to remove session data:
<?php
session_start();
if ($criteria_for_session_deletion === true) {
$_SESSION = []; // _SESSION is now an empty array
}
This will remove any value from the superglobal. It will not change the identity of the superglobal, but that shouldn't be important if the variable is now empty.
It is unclear from your question but you may be having overlap issues with browser caching of the outputted HTML page. Please clarify exactly what you're trying to delete?
Clicking on a "back button" is a very problematical way of solving this concept and we really need some clarification from you as to what's actually going on.
If you have a user who needs to have session data removed then you should check this in PHP on a script before any outout is sent to the browser, and then triggering the above code when required.
You maybe should have a "validity check" script included in each page so every time one of these pages is loaded your "check script" is called, and deletes the session data when the deletion criteria is met.
Why do you want to destroy session? That is just irritating. I have seen such implementations in government/bank websites and it pretty much sucks.
Rather you should redirect the user to dashboard if the user is logged in.
This doesn't directly answer OP's question but is a better way.
Something like this:
if (isset($_SESSION)) {
header('Location: <dashboard-page>');
exit;
}

Set cookie, nothing is being shown

This is taken directly from w3's website. I may not be understanding cookies correctly, but why is nothing displaying?
$expire=time()+60*60*24*30;
setcookie("user", "Alex Porter", $expire);
echo $_COOKIE["user"];
Your cookie will only be accessible when you refresh the page or navigate to a new one.
When your script loads, the HTML header fields for that page have already been set. The page will need to be rendered again (another HTTP transaction) before your cookie is available for use. Check PHP's documentation:
Once the cookies have been set, they can be accessed on the next page load with the $_COOKIE or $HTTP_COOKIE_VARS arrays.
check that your browser allows localhost / 127.x.x.x cookies or not ? if it allows then refresh the page. If you are using Google Chrome then you can see all browser cookies from here : chrome://settings/cookies navigate to localhost / 127.x.x.x to see your code has put cookies or not !
The variable $_COOKIE[] representates the state at the start of the script. That means that you have to wait on the next page request to see the variable. You could also add your variable manually to the global cookie variable $_COOKIE['user] = 'Alex Porter'; but the problem is that you are not sure that the browser really accepted the cookie.

Check for first visit of a site

Guys how can I check when the users first visited the website? because I want to display a pop up message when he/she first visited the website.,I found this question and this website http://www.electrictoolbox.com/jquery-cookies/ but I don't know how to use it. I just wrote a simple code in order for me to check how it is done.
<script type="text/javascript">
$.cookie("example", "foo");
alert( $.cookie("example") );
</script>
but its not working. What I am doing wrong here? or maybe you can suggest for another method. Any help would be much appreciated. Thanks.
You first need to check whether the cookie exists, and if so do the message (please don't use alert for that), and then set the cookie. E.g.:
if (!$.cookie("yourcookie")) {
// Show a message (please don't use alert)
}
$.cookie("yourcookie", "anything not blank here");
Of course, this only checks that the user doesn't have the cookie, it doesn't necessarily mean they've never been to the site before (as users can clear cookies).
download the js from this URL https://github.com/carhartl/jquery-cookie and import into your code
you can set cookie $.cookie("example", "foo"); OR ($.cookie("example", "foo", { expires: 7 }); - cookie last for 7 days
)
you can retrive the cookie by $.cookie("example");
using these
if(!$.cookie("example"))
{
alert('not 1st time');
}
{
alert('1st time');
$.cookie("example", "foo"); //set the cookie
}
On index page check if some cookie exist, if not set cookie to indefinite time and next time user returns to page you will know that he already visited (if he didn't remove cookies from browser). You could also put his IP address with some other data in your DB and check from DB if user has visited before, but it is also unreliable because there are ways you can change your IP also, depends on what are you trying to achieve and how important it is to know if user has visited page before.

How can I delete PHP cookies without refreshing the browser?

I want to delete a cookie but find the browser must be refreshed or another link clicked for the cookie to go away. I have used header(..) in PHP.
if(isset($_COOKIE['auth_key'])){setcookie("auth_key", "", time() - 3600);}
header("Location: ../login.php");
When I get to the login page, the cookie outputs, but on refresh it disappears, or if I go to another link from there, it disappears.
I would like the cookie removed without any user interaction and deleted before the server loads login.php.
Any help would be appreciated.
This answer is:
if(isset($_COOKIE['auth_key'])){setcookie("auth_key", "", 1,'/');}
because I set the cookie with a slash '/'. I used this to delete it, and it works now. However, it was odd that the cookie was still deleted on refresh.
Just clear the cookie (using this code) at the start of login.php instead of this redirect page.
Consider setting maybe a timeout period and then use Javascript to initiate an AJAX request to a server-side page to delete the cookie in question.
The correct answer I found was to set the cookie with a "/":
if(isset($_COOKIE['auth_key'])) {
setcookie("auth_key", "", 1, '/');
}
I used this to delete all cookies and it works now. However the cookie is still deleted on refresh.

Destroy PHP session on page leaving

I need to destroy a session when user leave from a particular page. I use session_destroy() on the end of the page but its not feasible for me because my page has pagination. My page is: abc.php?page=1 or abc.php?page=2 or abc.php?page=3.
So, I need to destroy a session when a user leaves from abc.php page. How can I do it without using a cookie?
Doing something when the user navigates away from a page is the wrong approach because you don't know if the user will navigate to a whole different page (say contact.php for the sake of the argument) or he/she will just go to the next page of abc.php and, as Borealid pointed out, you can't do it without JS. Instead, you could simply add a check and see if the user comes from abc.php:
First, in your abc.php file set a unique variable in the $_SESSION array which will act as a mark that the user has been on this page:
$_SESSION['previous'] = basename($_SERVER['PHP_SELF']);
Then, add this on all pages, before any output to check if the user is coming from abc.php:
if (isset($_SESSION['previous'])) {
if (basename($_SERVER['PHP_SELF']) != $_SESSION['previous']) {
session_destroy();
### or alternatively, you can use this for specific variables:
### unset($_SESSION['varname']);
}
}
This way you will destroy the session (or specific variables) only if the user is coming from abc.php and the current page is a different one.
I hope I was able to clearly explain this.
To trigger when the user actually leaves the page, you must use Javascript to send an asynchronous request back to the server. There's no way for the server to magically know the user has "left" a page.
See http://hideit.siteexperts.com/forums/viewConverse.asp?d_id=20684&Sort=0 .
I had a similar issue but mine was on a page reload I wanted variables that I had printed to be destroyed. It was for my login for my web design class I was making error feed back for if user put in a bad username or password. I could get the error to display but if I hit refresh page they errors would just stay there. I found that by just setting the variable to nothing after it printed would kill it. Take a look at what i did:
<p>To access my website please Login:</p>
<form name='login' action="./PHP_html/PHP/login.php" method='post'>
Username: <input type='text' name='username' /><div><?php print $_SESSION['baduser']; $_SESSION['baduser'] = "";?></div><br />
<div style="padding-left: 4px">Password: <input type='password' name='password' /><div><?php print $_SESSION['badpass']; $_SESSION['badpass'] = "";?></div></div>
<input type='submit' value='Login' /> or you can Register
I don't know if this helps at all but it worked for me.
Also, thanks to all you that post on sites like this to help those of us who are still learning.
For a particular page you need to destroy the session, then unset the all session variable
using
unset($_SESSION['varname']);
For the whole site you can use session_destroy();
I solve the problem.First take the current url then chk the page stay on current url.if page is not in the current url then destroy the session.
$url = "http" . ((!empty($_SERVER['HTTPS'])) ? "s" : "") . "://".$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'];
$page_name="abc.php";
if (!preg_match("/$page_name/",$url))
{
session_destroy();
}
But this code should be used on another pages.Because http is a stateless processes so no way to find when a user leave the page.
You can't tell when a user navigates away from the page, it's simply not possible in any reliable manner.
The best you can do is exploit how cookies work. When starting a session, you're sending a cookie to the client which identifies the client on each subsequent visit, and hence activates the associated session. It is up to the client to send this identification on subsequent visits, and it's up to the client to "forget" his identification.
You can instruct the client to only send the cookie for certain pages, and you can instruct him to forget the cookie when closing the browser (with a lifetime of 0). This can be set using session_set_cookie_params.
Other than that, you can simply ignore the session parameters on pages where they don't matter. You can delete the session (or certain values of it) after some time of inactivity when you assume the client has left.
Borealid deserves credit for pointing to the most elegant solution.
A more kludgey solution is to keep an iframe on the page that is pointed to another "monitor" page which is set to refresh every few seconds. This can be done without JavaScript using:
<meta http-equiv="refresh" content="10">
This refreshes the monitor page every 10 seconds. When this happens, the monitor page can record the time (overwriting the previously recorded time) and session ID on the server somewhere (DB or file).
Then you would have to create a cronjob that checks the file/DB for any sessions that are more than 10~12 seconds old and delete them manually. The session data is usually stored in a directory (specified by your PHP config) in a file named sess_the-session-ID. You could use a PHP function like this:
function delete_session($sessId) {
$sessionPath = session_save_path();
// you'll want to change the directory separator if it's a windows server
$sessFile = "$sessionPath/sess_$sessId";
if (file_exists($sessFile) && unlink($sessFile)) return true;
return false;
}

Categories