Use PHP sessions with jquery

后端 未结 7 1426
我寻月下人不归
我寻月下人不归 2021-01-26 10:02

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 someho

相关标签:
7条回答
  • 2021-01-26 10:14
    <script type="text/javascript">
        <?php if($admin == true || gebr_id == $_SESSION['id']){ ?>
            alert('<?php print $admin; ?>');
        <?php } ?>
    </script>
    

    also works.

    0 讨论(0)
  • 2021-01-26 10:15

    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

    0 讨论(0)
  • 2021-01-26 10:17

    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.

    0 讨论(0)
  • 2021-01-26 10:19

    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);
        }
      }
    }
    
    0 讨论(0)
  • 2021-01-26 10:26
    if($admin == true || gebr_id == $_SESSION['id']){
    $javascript = "javascript code here";
    }
    

    and then in your html

    <body>
    <?php echo $javascript ?>
    </body>
    
    0 讨论(0)
  • 2021-01-26 10:30

    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

    0 讨论(0)
提交回复
热议问题