Jump to content

[SOLVED] Ending a Session before starting a new one


jeffrydell

Recommended Posts

I have written an application which will be used by many websites, each one will 'approach' my php script with a unique identifier allowing me to establish where the site visitor is coming from, and allowing custom content to be presented based on the referring site.

 

(The referring site would have a link to:)

 

www.domain.com/entrypage.php?referrer=45

 

...where my site is 'domain.com' and the referring site's id (45) is stored in a MySQL table.

 

So far, so good.  When they hit 'entrypage.php', a session is initiated (session_start() ... again no problem) and the id is stored in $_SESSION['referrer'] for access by other scripts throughout my application.

 

Still doing OK up to here.

 

BUT ... what about when a person leaves, then accesses my site through a different referring site?  That's where I'm starting to see an issue, because they are looking at my application with the old session still running (apparently).

 

So in the 'entrypage' script, I'd like to kill all previous sessions BEFORE starting the new one. 

 

How do I do that?

 

Thanks in advance!

Link to comment
Share on other sites

Google -> php session -> Search -> PHP: Session Handling Functions - Manual <click>

 

Scroll down:

<snip>
Table of Contents

session_cache_expire — Return current cache expire
session_cache_limiter — Get and/or set the current cache limiter
session_commit — Alias of session_write_close()
session_decode — Decodes session data from a string
session_destroy — Destroys all data registered to a session
session_encode — Encodes the current session data as a string
session_get_cookie_params — Get the session cookie parameters
session_id — Get and/or set the current session id
session_is_registered — Find out whether a global variable is registered in a session
session_module_name — Get and/or set the current session module
session_name — Get and/or set the current session name
session_regenerate_id — Update the current session id with a newly generated one
session_register — Register one or more global variables with the current session
session_save_path — Get and/or set the current session save path
session_set_cookie_params — Set the session cookie parameters
session_set_save_handler — Sets user-level session storage functions
session_start — Initialize session data
session_unregister — Unregister a global variable from the current session
session_unset — Free all session variables
session_write_close — Write session data and end session

 

Reading carefully, we notice some keywords:

session_destroy — Destroys all data registered to a session

 

Clicking on session_destroy gives us information on destroying a session, as well as examples in the manual and contributed by the community.

 

I don't mean to be rude, but it sounds like you have a somewhat complicated system going.  So how is it you managed to not know how to use Google?

Link to comment
Share on other sites

roopurt18,

 

First ...

 

"I don't mean to be rude, but it sounds like..."

 

A long time ago I learned that everything preceding the word "but" is a lie.

 

Now, as for WHY I didn't just use session_destroy() (which, by the way, I looked it up on the php manual website) the manual states:

 

session_destroy() destroys all of the data associated with the current session. It does not unset any of the global variables associated with the session, or unset the session cookie.

 

That second part is the rub ... I didn't know if there was a better way which would unset the global variables associated with the session and unset the session cookie as well.

 

Yes, it's a fairly complicated project I'm working on.  I'm also 'self-taught' (with a little assistance & guidance from helpful people ... some of which use this forum) and I'm learning a lot along the way.  I'm sure with your rather extensive knowledge of php you understand that there is a lot to learn and MOST people don't know it 'all' as soon as they start writing code. 

 

The single hardest part of learning a language and everything that comes with something like php is finding the correct term for what you need.  Without knowing the correct terminology, it's rather difficult to know if you've "looked up" all the possible information relating to what you're doing.  Such was the case here.

 

If 'our' questions are annoying or simplistic to you - I suggest that you might consider going out and getting more freelance work so you can show off your "brilliance" for pay ... perhaps creating something worthwhile in the process ... rather than 'dumping' on those of us who are honestly trying to learn and want to use this community for what it was intended.

 

Oh, and by the way - I DID mean to be rude.  Have a nice day.

Link to comment
Share on other sites

<?php
// Initialize the session.
// If you are using session_name("something"), don't forget it now!
session_start();

// Unset all of the session variables.
$_SESSION = array();

// If it's desired to kill the session, also delete the session cookie.
// Note: This will destroy the session, and not just the session data!
if (isset($_COOKIE[session_name()])) {
    setcookie(session_name(), '', time()-42000, '/');
}

// Finally, destroy the session.
session_destroy();
?> 

 

Straight out of the manual for session_destroy.  It shows how to clear the session data variables and the cookie as well, which was the rub for you.

 

A long time ago I learned that everything preceding the word "but" is a lie.

You learned wrong.  Example: "Water can be cold but it can also be hot."

 

If 'our' questions are annoying or simplistic to you

If it were annoying I wouldn't have responded; if it were simplistic I wouldn't have show you how I found the answer when I originally had this question myself.

 

rather than 'dumping' on those of us who are honestly trying to learn and want to use this community for what it was intended.

Learning how to program is more than learning a language, it's learning how to become more resourceful.  You can always find the answers to the simplistic questions because they've been asked dozens of times before; but you need to develop the skills to uncover the answers to questions that cause hair loss.  I post questions occasionally, however most of them fall into more advanced topics; as such, by the time someone answers I've usually found a solution or work-around I can apply to my situation, but not always.  The only reason I responded the way I did is because it seemed to me that if you had dug around for the extra amount of time that you spent writing the post, you would have probably found the solution.  Even typing session_destroy into the search function for this site brings up several results that might have lead you to a resolution.

 

Oh, and by the way - I DID mean to be rude.

 

Have a nice day.

I will, thanks!

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.