I'm at the begining of my jQuery jouerny and I still have difficult times understanding some basic topics so I decided to make a simple script with just a few lines of PHP to try some things. I have a main page:
<?php session_start(); ?>
<!DOCTYPE html>
<html>
<head>
<title> TEST </title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
</head>
<body>
<a href = "http://www.abv.bg/" class="isLogged">abv.bg</a>
<br />
<hr />
<a href = "http://www.yahoo.com/" class="isLogged">Yahoo!</a>
<br />
<hr />
<a href = "http://www.southparkstudios.com/" class="isLogged">South Park</a>
<br />
<hr />
<a href = "http://www.youtube.com/" class="isLogged">Youtube</a>
<br />
<hr />
<a href = "http://www.vbox7.com/" class="isLogged">Vbox</a>
<br />
<hr />
<a href = "http://www.sa.dir.bg/" class="isLogged">Dir.BG</a>
<br />
<div class="isLogged"> hi<div>
<script type="text/javascript">
$(document).ready(function(){
var Status = <?php echo $_SESSION['isLogged'] ?>
$('.isLogged').click(function(){
if(Status!=true){
var Check = prompt('Enter Password', '');
$.post('check.php', {Check:Check}, function(data) { var result = data;
alert(result); exit;
});
}
});
});
</script>
</body>
</html>
the improtant part is in the tags. As you can see I want to make a very simple check if user has entered a password and to let him open link with class="isLogged" if he is. What I have hard time to understan is how to manage my php side script, here is what I have now:
<?php session_start();
$data = $_POST['Check'];
$yes = "[{data}]";
$no = 'N';
if ($data != 'Ivan')
{
echo json_encode($yes);
}
else
{
$_SESSION['isLogged'] = true;
}
?>
But I don't know how to format my data on the PHP side so I can make a proper check in the script. However I've reached that far that at least echo json_encode($yes);
so I use it to make my check, but still, I can not find out how to prevent the link from loading.
So it makes a lot of questions buthope it's not that hard to get the correct answers.
thanks
Leron
You could do it like this. I think this is what you're trying to do: check for the correct password before allowing the user to proceed to the link that was clicked.
<script type="text/javascript">
$(document).ready(function(){
var Status = <?php echo (bool)$_SESSION['isLogged']; ?>
$('.isLogged').click(function(){
if(!Status)
{
var Check = prompt('Enter Password', '');
var Success = false;
$.post('check.php', {Check:Check}, function(data) {
var response = $.parseJSON(data);
if(response.result)
{
Success = true;
}
});
/*
If you return false in a .click() it stops the link being visited. The browser won't follow the href.
So here, if the password was wrong, Success will be false and thus the link won't be visited
*/
return Success;
}
});
});
</script>
PHP side:
<?php
session_start();
$out = array('result' => false);
if($_SERVER['REQUEST_METHOD'] == 'POST')
{
$password = 'Ivan';
$input = $_POST['Check'];
if(strcmp($input, $password) === 0) // case sensitive string comparison, 0 = strings are same
{
$_SESSION['isLogged'] = true;
$out['result'] = true;
}
}
echo json_encode($out);
?>
Disclaimer: I appreciate this is a learning exercise rather than an actual system, this is obviously not a secure way of protecting content because we can't stop the user from browsing the link if they want to, or disabling javascript.