Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Jackie Jen
2,723 Pointswhen a user click the cross button on top of window tab, will automatically update on mysql database.
I have a situation above:
when a user go to view.php page i will update my mysql database one of the column "view". when a user click the view another page or click the close button on top of window tab i will update the database "view" to "no" since to check the user is leaving the page should be done on client side i'm using javascript then using ajax to update my database.
below is my code
window.onbeforeunload = confirmExit;
function confirmExit()
{
//update the database
}
Since i'm closing the page it will closing the connection to the server and therefore it might won't wait Ajax call to finish. Is there more efficient way to make sure it can update the database when user leaving the current page
Jackie Jen
2,723 PointsHi MicL,
Let said they close all the browser, which mean there is no connection to my server. How can my php script still can be checking the $_COOKIE?
Regards
1 Answer
Al3n MicL
6,364 PointsRemember the $_COOKIE is a php super global that contains all the cookie data that you define in your site using setcookie(). It stays active in your server cache instance until either the session is terminated by the user or the cookie(s) expires. Cookies are a two way communication exchange between server and client. Here's a more thorough explanation on using cookies
Hope that helps.
Jackie Jen
2,723 PointsThanks alot MicL, that clear my worries.
Al3n MicL
6,364 PointsAl3n MicL
6,364 PointsHmm, that's a tricky one. Sounds to me like you are trying to implement a user session functionality where a user can view certain pages, presumably by logging in. What I would suggest is creating a log-in page and creating a database table for user's who log-in/ register. Once that action is complete, initiate a cookie that auto-expires once no user activity is seen for about an hour. That way regardless if a user navigates away from your page (or closes their browser) the $_COOKIE will time out on the server. Create some php script that checks for this cookie associated with the user. If it has expired, then update your database accordingly.
Browse through the php manual sections on sessions and setting cookies for more info.