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????
$_SESSIONstate) 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.