Jump to content

How to use PHP to delete cookies when browser is close.


iconicCreator

Recommended Posts

I have a script, (Javascript) attached to my page.

 

This script basically process a style sheet switcher that applies different style sheets to the page based on the users choice.

 

This is my problem and I have tried all I know.

 

I need the page to go back to the default style when the user closes the browser.

How to do this? I have no idea!

 

What I understand is that I have to find a way to remove/delete/distroy cookies when the user closes the browser. So that when they return, the page is back to normal style.

 

Is it possible to use PHP to destroy/delete the cookies created by this script?

Or are these cookies saved to my computer hardrive?

 

If so, then how?

 

Thanks everyone for your time a patience.

 

IC

Link to comment
Share on other sites

I have a script, (Javascript) attached to my page.

 

This script basically process a style sheet switcher that applies different style sheets to the page based on the users choice.

 

This is my problem and I have tried all I know.

 

I need the page to go back to the default style when the user closes the browser.

How to do this? I have no idea!

 

What I understand is that I have to find a way to remove/delete/distroy cookies when the user closes the browser. So that when they return, the page is back to normal style.

 

Is it possible to use PHP to destroy/delete the cookies created by this script?

Or are these cookies saved to my computer hardrive?

 

If so, then how?

 

Thanks everyone for your time a patience.

 

IC

 

do you have to use cookies could you not use $_SESSION['name'] instead?

$_COOKIES['name'] are persistent (and tasty) where $_SESSION['name'] timeout shorty after user leaves - dependant on php.ini settings and browser I belive

Link to comment
Share on other sites

lol I am tired and keep having after thoughts

 

 

when cookies are stored on the clients harddrive, they are dependant of the clients pc.

 

if a client uses your script which I assume sets cookies based on their style choice the page will look like that for them only untill they delete the cookie or connect from a different pc.

 

the page will only look like that for them and not others that connect.

 

if you wanted to avoid this (they would need to change the style each time)

 

you could use a small function to delete all the cookies before the page loads.

either using javascript or the unset function I mentioned above.

Link to comment
Share on other sites

lol I am tired and keep having after thoughts

 

 

when cookies are stored on the clients harddrive, they are dependant of the clients pc.

 

if a client uses your script which I assume sets cookies based on their style choice the page will look like that for them only untill they delete the cookie or connect from a different pc.

 

the page will only look like that for them and not others that connect.

 

if you wanted to avoid this (they would need to change the style each time)

 

you could use a small function to delete all the cookies before the page loads.

either using javascript or the unset function I mentioned above.

 

Thanks very much for you time,

However, as I mentioned, the page does not contained a PHP variable, ad I'm just confused,

 

Because it's running a javascript script. So how do I set a name?

What will be a cookie/session be named?

 

That's where I'm confused as to what will be the session name or cookie name, sine I have no PHP variable.

 

IC

Link to comment
Share on other sites

lol I am tired and keep having after thoughts

 

 

when cookies are stored on the clients harddrive, they are dependant of the clients pc.

 

if a client uses your script which I assume sets cookies based on their style choice the page will look like that for them only untill they delete the cookie or connect from a different pc.

 

the page will only look like that for them and not others that connect.

 

if you wanted to avoid this (they would need to change the style each time)

 

you could use a small function to delete all the cookies before the page loads.

either using javascript or the unset function I mentioned above.

 

Thanks very much for you time,

However, as I mentioned, the page does not contained a PHP variable, ad I'm just confused,

 

Because it's running a javascript script. So how do I set a name?

What will be a cookie/session be named?

 

That's where I'm confused as to what will be the session name or cookie name, sine I have no PHP variable.

 

IC

 

a cookie is a cookie regardless of what creates it.

if your javascript created a cookie called hello like so

 document.cookie = 'hello = world'; 

your php cookie would be called

$_COOKIE['hello'];

 

 

Javascript Erase Cookie for cookie named hello

function eraseCookie(name) {
createCookie(name,"",-1);
}

eraseCookie(hello)

 

PHP erase cookie

unset $_COOKIE['hello'];

 

so at the start of your page you could use

    <?php
         function DeleteCookie ($name)
         {
              if (isset($_COOKIE[$name]))
             {
                 unset($_COOKIE[$name])
             }
         }

         DeleteCookie("CookieOne");
         DeleteCookie("CookieTwo");
     ?>

      //If this is only thing that sets cookies you could just grab and delete all cookies
     <?
         function DeleteAllCookies()
         {
           if (isset($_SERVER['HTTP_COOKIE'])) 
           {
              $cookies = explode(';', $_SERVER['HTTP_COOKIE']);
              foreach($cookies as $cookie) 
              {
                 $parts = explode('=', $cookie);
                 $name = trim($parts[0]);
                 unset($_COOKIE[$name])
              }
          }

          DeleteAllCookies();
     ?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.