0

I do not know if there is a better way to do this but I use this way (tell me if I am wrong) I want to make some JavaScript to show something if the user is logged in and hide that thing if the user is not logged in. but the function that did the log in credentials check is written in PHP :

function login()
{
    $username = $_POST['username'];
    $password = md5($_POST['password']);
    $url = $_POST['url'];
    $users = $GLOBALS['db']->query("SELECT * FROM users WHERE username='$username' AND password='$password'") or $GLOBALS['db']->raise_error(); // Leaving 'raise_error()' blank will create an error message with the SQL
    $users_number = $GLOBALS['db']->num_rows($users);
    if(!empty($users_number))
    {
        $_SESSION['username'] = $username;
        $_SESSION['password'] = $password;
            $_SESSION['is_logged'] = 'yes';
        header('Location:?'.$url);
    }

}

I am trying to get and check this value of $_SESSION['is_logged'] in JavaScript but I could not is there a way to pass or to read this value from jquery or javaScript

js file:

$(".link_to_comment a").live('click',function(){
                //if you are not logged in you will see a log in box (with a link to register if you are not)
                if(session == 'no')
                {
                    $("#forum").html("<form name='login_form' action='?page=functions.php&fun=login' method='post'><table><tr><td>Username:</td><td><input type='text' name='username'></td><td>error</td></tr><tr><td>Password:</td><td><input type='password' name='password'></td><td>error</td></tr><tr><td></td><td><input type='submit'></td><td>error</td></tr><tr><td></td><td><input type='hidden' name='url' value="+url+"></td><td></td></tr></table>");
                }
                //if you are logged in you will add your comment here
                else if(session == 'yes')
                {
                    $("#forum").find(".make_a_comment").show();
                    $("#forum").find(".link_to_comment").hide();
                }
            });

the session varibale in js file should contain $_SESSION['is_logged'] but how this is my question????

1
  • 1
    Consider showing/hiding (rendering) the output via PHP (i.e., on the server side, based on the $_SESSION state) instead of transferring the session state to the client and only then make DOM manipulations. Just a thought. Oh, and you may want read up on SQL injections. Commented Jun 26, 2013 at 7:07

4 Answers 4

3

You could output the variable as a <script> within the page itself, in the global scope. This would give any external Javascript files access to the variable:

<script>
    var loggedIn = <?php echo isset($_SESSION['is_logged']) && $_SESSION['is_logged'] == 'yes' ? 'true' : 'false'; ?>;
</script>

This would output var loggedIn = true; or var loggedIn = false; depending on the session variable.

Now in any other Javascript you can check the variable:

            if(loggedIn == false)
            {
                $("#forum").html("<form name='login_form' action='?page=functions.php&fun=login' method='post'><table><tr><td>Username:</td><td><input type='text' name='username'></td><td>error</td></tr><tr><td>Password:</td><td><input type='password' name='password'></td><td>error</td></tr><tr><td></td><td><input type='submit'></td><td>error</td></tr><tr><td></td><td><input type='hidden' name='url' value="+url+"></td><td></td></tr></table>");
            }

Keep in mind that you need to output the variable before any other Javascript, otherwise the variable might not exist before the other script tries to access it.

Sign up to request clarification or add additional context in comments.

3 Comments

I added the code to the login function but when I tried to alert it in the js file the console said undefined variable. I remove the var keyword to make it global but it never works
Added to the login function? that doesn't sound right. You need to add the <script></script> to every page, preferably in the <head> section, before any other Javascript.
thank you very much I added to the head and it it working now
2

yes you can assign value to on global variable in script tag then you can use in jquery function with variable name

var isUserLoggedIn = <?php echo ($_SESSION['is_logged']) ? "true" : "false"; ?>

let me know if i can help you more.

1 Comment

I tried that but it gives me undefined variable when I tried to alert the value in js file .even I remove the var keyword but not working
2

Your js code:

session = "<?php echo  $_SESSION['logged_in']; ?>"; 

since I have not used var so it means it is globally available in all js files.

alert(session); //will return you the value of S_SESSION

Comments

0

You could create dynamic Javascript code through PHP, i.e.:

var isLogged = <?php echo (isset($_SESSION['is_logged']) && $_SESSION['is_logged']) ? "true" : "false"; ?>

This way, you can dynamically create a Javascript variable isLogged that you can use in your Javascript code

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.