Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: Javascript PHP
I have the following code in php:
 
echo '<head><script type="text/javascript">function EndSession() {
		var _endSession = confirm("End current editing session ?");
		if(_endSession == true)
		{';
				unset($_SESSION['UsernameC']);
			echo 'alert("Session terminated.");
			window.location = "index.php?page=acp";
		}
		else
		{
			alert("Session active.");
		}
	}</script></head>';
 
You can notice the ' unset($_SESSION['UsernameC']); ' is part of the php script, that ment to run only inside the javascript, but it runs anytime, is there any way to fix it ?
Posted 16-Apr-13 3:26am

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

unset($_SESSION['UsernameC']);
 
This is server-side code, and will run when the page is being "rendered" in server to be sent to the client.
 
Your javascript runs in the client side, that don't have direct access to the server-side code.
 
Maybe you could create a logout page that remove the user session and then redirects him to the index page or you could do the session logout through an Ajax call.
 
What you need to have in mind is:
 
PHP -> server-side code
 
javascript -> client-side code
 
and that client and server code don't have direct access to each other.
 
Hope it helps.
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 6,564
1 Sergey Alexandrovich Kryukov 6,078
2 DamithSL 5,228
3 Manas Bhardwaj 4,717
4 Maciej Los 4,150


Advertise | Privacy | Mobile
Web03 | 2.8.1411022.1 | Last Updated 16 Apr 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100