A community in which webmasters can ask for help with topics such as PHP coding , MySQL , IT jobs, web design, IT security.
Current location:homephp forumphp talk in 2009 yearRead session value with Javascript - page 1
User InfoPosts
Read session value with Javascript#1
Is it possible to read a session value with Javascript?

For example, if I assigned a value into a session in PHP:

$_SESSION[(msg(] = "ABC Message";


Is it possible to read $_SESSION[(msg(] with Javascript?

posted date: 2009-04-15 20:30:00


Re: Read session value with Javascript#2
I had made out the solution of this problem. click to view my topic...

hope that hepls.

posted date: 2009-04-15 20:30:01


Re: Read session value with Javascript#3
$_SESSION is a server-side construct. You would need to store that variable in $_COOKIE to be able to access it client-side.

posted date: 2009-04-15 20:36:00


Re: Read session value with Javascript#4
.. Or you can use ajax to retrive your server side session value into you client-side javascript.`(quick, dirty and untested example, using jQuery)Javascript Side:$.ajax({ url: "test.php", cache: false, success: function(html){ eval( html ); /// UGLY NASTY YOU MUST VALIDATE YOUR INPUTS... JUST AN EXAMPLE } });PHP side test.php:echo (var myData = "(. $_SESSION[(msg(].("(;

posted date: 2009-04-15 21:21:00


Re: Read session value with Javascript#5
A very simple way is to generate the JavaScript with some PHP code:<script type="text/javascript"> <?php echo (var msg = "(.json_encode($_SESSION[(msg(]).(";(;</script>

posted date: 2009-04-15 21:57:00


Re: Read session value with Javascript#6
If $_SESSION['msg'] stores sensitive information, outputting to an unencrypted page is a security risk, and HTTPS should be used.

posted date: 2009-04-16 04:16:00


Re: Read session value with Javascript#7
you could use json_encode() to properly escape it

posted date: 2009-04-16 06:46:00


Re: Read session value with Javascript#8
@tomhaigh Thanks, updated accordingly

posted date: 2009-04-16 14:41:00


Re: Read session value with Javascript#9
Why is this being voted up? harto's solution proves it untrue.

posted date: 2009-04-16 14:46:00


Re: Read session value with Javascript#10
Because the first part of the answer is quite correct.

posted date: 2009-04-16 14:47:00


Re: Read session value with Javascript#11
Yes agreed, but it doesn't answer the question and is in fact presenting misleading information.

posted date: 2009-04-16 15:28:00


Re: Read session value with Javascript#12
How is this misleading? You cannot read a session variable with javascript. You either have to stuff the value into a cookie, which javascript could access, or have it rendered out to the page itself. That is not very secure.

posted date: 2009-04-16 19:19:00


select page: « 1 2 »
Copyright ©2008-2017 www.momige.com, all rights reserved.