2

I am having trouble figuring out how to create a javascript function that will destroy a php session. I have a clickable link that will call a function called destroyphpsess. I think this is all right so far. Now I need to define the javascript function. This is the code I have so far...

if ($_SESSION['color'] == "") {
    $var = "<a href='JavaScript:newPopup(\"http://www.yourfantasyfootballreality.com/register.php\");' class='two'>Register</a> | <a href='JavaScript:newPopup(\"http://www.yourfantasyfootballreality.com/signin.php\");' class='two'>Sign In</a>";
} else {
    $var = "Hello, ".$_SESSION['color'] ."! | " . "<a href=\"http://www.yourfantasyfootballreality.com/index.php\" onclick=\"destroyphpsess()\" class='two'>Log Out</a>";
}
echo $var;

Now I need to define the javascript function. This is where I am having trouble. This is the basic outline I have so far...

function destroyphpsess()
{
<?php
session_destroy();
?>
}

If someone could help me with the function I would appreciate it! Thanks.

2
  • Why dont you use ajax and call a page which will destroy the session Commented Jun 16, 2012 at 4:09
  • 3
    +1 for coming back with a much better question! :D Commented Jun 16, 2012 at 4:33

4 Answers 4

11

Your JavaScript runs client-side, and your PHP runs server-side. You can't call PHP functions from JavaScript this way. You have two options:

  1. Do an AJAX call to a server-side script that clears the session data (recommended, for consistency and proper clearing of that stuff server-side)
  2. Clear the PHPSESSID cookie with JavaScript (won't work if you reconfigure how you handle sessions, or if sessions are handled by URL parameters)
Sign up to request clarification or add additional context in comments.

1 Comment

I'm pretty sure you meant JavaScript runs client-side and PHP runs server-side.
6

A PHP session generally uses a cookie that is stored client side. The following code will clear that cookie thus unlinking the session.

document.cookie = 'PHPSESSID=; expires=Thu, 01-Jan-70 00:00:01 GMT;';

13 Comments

+1 I'd use session_destroy from an AJAX call... I would never thought of doing this through cleaning a cookie, but I liked the idea.
That was my other thought, but yeah i was thinking all client side. This code will leave the session hanging on the server side until it expires
where would i put this line of code? would i put it in the function?
You would put this code at the point of which you want to end the session. This is javascript. So maybe put it in a logout event handler of something of that sort
I want the user to be able to click on a link and for the session to instantly be destroyed while staying on the same page. So where would the above code go in reference to what I already have?
|
0

Everyone keeps saying AJAX. This would be a good idea if you want to clean up the cookie on the server side as well, but I think if you knew what AJAX was you probably would have used it already...

I found AJAX pretty confusing when I first came across it. So what I would suggest is having a look at and maybe using jQuery's AJAX functionality. Including jQuery for just one function is probably overkill, but it might be a bit easier.

Comments

0

Your JavaScript runs client-side, and your PHP runs server-side. You can't call PHP functions from JavaScript this way. You have two options:

Do an AJAX call to a server-side script that clears the session data (recommended, for consistency and proper clearing of that stuff server-side) Clear the PHPSESSID cookie with JavaScript (won't work if you reconfigure how you handle sessions, or if sessions are handled by URL parameters)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.