0

I created a simple PHP login authentication script, now i want to override the posting of data from the form to the auth script so that it does it via ajax.

I made it so that the PHP returns JSON data in the format {"success":[1 OR 0],"error";"ANY ERROR MESSAGE RELATED TO SUCCESS = 0"}, this works perfectly and when i try and use the login form it returns success:1 when i serialize the form and post it to the auth script with a correct username+password and it returns success:0 and the reason why with an incorrect login, on the original method where the form posted to the auth script, then the auth script redirected to a protected page after succesful login i have replaced this with the javascript changing the window location after it recieves a succesful login, only problem being is that is seems the session variables havent been set from posting to the auth script via this method.

Is there a way i can make the session variables stick without actually having to direct to the page?

James T
  • 1,155
  • 4
  • 17
  • 39

2 Answers2

0

Creating & Accessing the Session Variable using JavaScript

Creating Session Variable using JavaScript

<?php session_start(); ?>
<html>
<head>
<script type='text/javascript'>
    function setSession(variable, value) {
        xmlhttp = new XMLHttpRequest();
        xmlhttp.open("GET", "setSession.php?variable=" + variable + "&value=" + value, true);
        xmlhttp.send();
    }
</script>
</head>
<body>
<?php
    if(isset($_SESSION['login']) && $_SESSION['login'] == "true")
      echo "Session Active. <a href=\"javascript:setSession('login', 'false')\"><input type='submit' value='De-Activate'></a>";
    else
      echo "Session Inactive. <a href=\"javascript:setSession('login', 'true')\"><input type='submit' value='Activate'></a>";
      echo "<a href=\"index.php\"><input type='submit' value='Re-Load Page'></a>";
?>
</body>
</html>

Assigning value to it

<?php
    session_start();
    if(isset($_REQUEST['variable']) && isset($_REQUEST['value']))
    {
        $variable = $_REQUEST['variable'];
        $value = $_REQUEST['value'];
        $_SESSION[$variable] = $value;
    }
?>
Vikrant
  • 4,920
  • 17
  • 48
  • 72
0

You can do something like this

<script src='javascript.php'></script>

Then in the script you can write this for accessing session variable

<?php header("Content-type: application/javascript"); ?>
$(function() {
    $( "#progressbar" ).progressbar({
        value: <?php echo $_SESSION['value'] ?>
    });

Source link

Community
  • 1
  • 1
avinash pandey
  • 1,321
  • 2
  • 11
  • 15