Reputation: 21
Well, my question is simple:
I wish to use my PHP sessions in jQuery, in an if statement, however I do not know how I can do this on an easy way. Am I supposed to somehow import the $_SESSION
in jquery?
What I basically want to do, is open a dialog if the User_ID is the same as the $_SESSION
User_ID. Also I wish to have it checked to see if someone is an administrator, shop-manager or a user, with the session. This information already exists in PHP and the User_ID only exists in jQuery.
if($admin == true || gebr_id == $_SESSION['id']){
Javascript code here?
}
I think that would be the easiest way to solve it, however that does not seem to work for me. Perhaps I am forgetting tags for the PHP to understand it's javascript?
I've figured out an easy way to come around the solution, thanks you you all!
Solution:
$('.do').click(function(){
<?php
if ($admin == true) {
echo "$('#dialog').dialog('open');\n";
echo "var cid = $(this).attr('id');\n";
echo "var datum = cid.substr(0, 10);\n";
echo "var naampos = cid.indexOf('|');\n";
echo "var gebridpos = cid.indexOf('||');\n";
echo "var naam = cid.substring(naampos+1,gebridpos);\n";
echo "var gebrid = cid.substring(gebridpos+2);\n";
echo "$.ajax({\n";
echo "type: \"POST\",\n";
echo "url: \"agenda.php\",\n";
echo "data: naam,\n";
echo "success: function(){\n";
echo "$('#gebruikerinput').html(\"<input type='text' READONLY='' size='35' value='\" + naam +\"'>\");\n";
echo "$('#gebridinput').html(\"<input type='hidden' name='gebridtextbox' value='\" + gebrid + \"'>\");\n";
echo "$('#datuminput').html(\"<input type='text' READONLY='' size='12' name='datum' value='\" + datum + \"'>\");\n";
echo "}\n";
echo "})\n";
echo "return false;\n";
}
?>
});
It seems to be working so far. The only thing left for me to do is to make so jQuery returns a User_ID variable which needs to be identical to the session ID.
Upvotes: 0
Views: 8261
Reputation: 647
If you like to get access to PHP Sessions from javascript, you can write a simple wrapper.
Somethign like this:
<?php
session_start($_GET['sid']);
echo json_encode($_SESSION);
?>
Keep in mind that this is a possible security flaw, as everyone can get access to your PHP Session variables from outside (with a valid session id).
Now you can simply use jQuery to ask the server for the current session data
Upvotes: 0
Reputation: 1566
Bypassing the apparently obvious security flaws with what you seem to be suggesting, you want to output the PHP session ID to your web page. Of course, JQuery is a client-side language, and it's pretty trivial to circumvent any client-side security of that nature.
<script type='text/javascript'>
<?php
//put in your variables from PHP
echo 'var phpId="' . $_SESSION['id'] .'";' ."\n";
echo 'var admin=' .$admin.";\n";
?>
//Back to Javascript (client side) here
if (gebr_id == phpId || admin){
...
}
</script>
You say you only have the user ID in JQuery. What you should do is code it so you have the user id available in PHP instead... Submit a form with the user id or something like that, then you can store that in the PHP session and do your authentication server-side.
Upvotes: 0
Reputation: 2810
<script type="text/javascript">
<?php if($admin == true || gebr_id == $_SESSION['id']){ ?>
alert('<?php print $admin; ?>');
<?php } ?>
</script>
also works.
Upvotes: 0
Reputation: 37136
Php is a server side language. javascript runs on the browser (client side).You can do this:
if($admin == true || gebr_id == $_SESSION['id'])
echo "<script>";
echo "here your jquery function";
echo "</script>";
}
So that you're just rendering HTML
hope it helps
Upvotes: 0
Reputation:
Sadly that you can mix server-side script (PHP) with client-side script (Javascript). However, you can use PHP to echo
Javascript, like:
if ($admin == true || gebr_id == $_SESSION['id']){
echo '<script>
alert("Admin!");
$("selector").function(); //
</script>';
}
Upvotes: 0
Reputation: 9415
You cannot import the $_SESSION, but you can store some of them (only those that you want) in the cookie and read it in javascript.
For example: store the user-id in the cookie and you can read the value from the cookie in javascript.
read cookie:
function getCookie(c_name)
{
var i,x,y,ARRcookies=document.cookie.split(";");
for (i=0;i<ARRcookies.length;i++)
{
x=ARRcookies[i].substr(0,ARRcookies[i].indexOf("="));
y=ARRcookies[i].substr(ARRcookies[i].indexOf("=")+1);
x=x.replace(/^\s+|\s+$/g,"");
if (x==c_name)
{
return unescape(y);
}
}
}
Upvotes: 0
Reputation: 391
if($admin == true || gebr_id == $_SESSION['id']){
$javascript = "javascript code here";
}
and then in your html
<body>
<?php echo $javascript ?>
</body>
Upvotes: 1