Set Session variable using javascript in PHP

后端 未结 9 915
刺人心
刺人心 2020-11-27 21:23

Is it possible to set PHP session variables using Javascript?

相关标签:
9条回答
  • 2020-11-27 21:46

    Not possible. Because JavaScript is client-side and session is server-side. To do anything related to a PHP session, you have to go to the server.

    0 讨论(0)
  • 2020-11-27 21:47

    If you want to allow client-side manipulation of persistent data, then it's best to just use cookies. That's what cookies were designed for.

    0 讨论(0)
  • 2020-11-27 21:55

    You can't directly manipulate a session value from Javascript - they only exist on the server.

    You could let your Javascript get and set values in the session by using AJAX calls though.

    See also

    • Javascript and session variables
    • jQuery click event to change php session variable
    0 讨论(0)
  • 2020-11-27 21:57

    or by pure js, see also on StackOverflow : JavaScript post request like a form submit

    BUT WHY try to set $_session with js? any JS variable can be modified by a player with some 3rd party tools (firebug), thus any player can mod the $_session[]! And PHP cant give js any secret codes (or even [rolling] encrypted) to return, it is all visible. Jquery or AJAX can't help, it's all js in the end.

    This happens in online game design a lot. (Maybe a bit of Game Theory? forgive me, I have a masters and love to put theory to use :) ) Like in crimegameonline.com, I initialize a minigame puzzle with PHP, saving the initial board in $_SESSION['foo']. Then, I use php to [make html that] shows the initial puzzle start. Then, js takes over, watching buttons and modding element xy's as players make moves. I DONT want to play client-server (like WOW) and ask the server 'hey, my player want's to move to xy, what should I do?'. It's a lot of bandwidth, I don't want the server that involved.

    And I can just send POSTs each time the player makes an error (or dies). The player can block outgoing POSTs (and alter local JS vars to make it forget the out count) or simply modify outgoing POST data. YES, people will do this, especially if real money is involved.

    If the game is small, you could send post updates EACH move (button click), 1-way, with post vars of the last TWO moves. Then, the server sanity checks last and cats new in a $_SESSION['allMoves']. If the game is massive, you could just send a 'halfway' update of all preceeding moves, and see if it matches in the final update's list.

    Then, after a js thinks we have a win, add or mod a button to change pages:

    document.getElementById('but1').onclick=Function("leave()");
    ...
    function leave() {
        var line='crimegameonline-p9b.php';
        top.location.href=line;
    }
    

    Then the new page's PHP looks at $_SESSION['init'] and plays thru each of the $_SESSION['allMoves'] to see if it is really a winner. The server (PHP) must decide if it is really a winner, not the client (js).

    0 讨论(0)
  • 2020-11-27 21:58

    One simple way to set session variable is by sending request to another PHP file. Here no need to use Jquery or any other library.

    Consider I have index.php file where I am creating SESSION variable (say $_SESSION['v']=0) if SESSION is not created otherwise I will load other file.

    Code is like this:

    session_start();
    if(!isset($_SESSION['v']))
    {   
        $_SESSION['v']=0;
    }
    else
    {
        header("Location:connect.php");
    }
    

    Now in count.html I want to set this session variable to 1.

    Content in count.html

    function doneHandler(result) {
       window.location="setSession.php";
    }
    

    In count.html javascript part, send a request to another PHP file (say setSession.php) where i can have access to session variable.

    So in setSession.php will write

    session_start(); 
    $_SESSION['v']=1;
    header('Location:index.php');
    
    0 讨论(0)
  • 2020-11-27 22:03

    In JavaScript:

    jQuery('#div_session_write').load('session_write.php?session_name=new_value');
    

    In session_write.php file:

    <?
    session_start();
    
    if (isset($_GET['session_name'])) {$_SESSION['session_name'] = $_GET['session_name'];}
    ?>
    

    In HTML:

    <div id='div_session_write'> </div>
    
    0 讨论(0)
提交回复
热议问题