So, I'm trying to create a log in function for a web app.
The login is handled by a JSON interface. The user enters a username and password which are checked, and if they are correct, the JSON interface sends back a token. This is where the session should start, I guess.
<script>
$(document).ready(function(){
$("#loginForm").validate({
rules: {
username: {
required: true,
digits: true
},
password: {
required: true
}
},
submitHandler: function(form) {
data = JSON.stringify({
"jsonrpc": "2.0",
"method": "login",
"params": {
"params": {
"username": $('#username').val(),
"password": $('#password').val()
}
}
});
$.ajax({
url:"http://domain.local:1234/trip",
type:"POST",
crossDomain: true,
dataType: "json",
data : data,
headers: {
'Content-Type': "application/json; charset=utf-8"
},
success: function(data){
if(data.result.token != null) {
window.location.href = "functions/login.php?token=" + data.result.token;
}
else $("#result").html("Invalid username and/or password.");
}
});
return false;
}
});
});
</script>
My login.php is where the session handling should occur (right?). If possible at all, I'd like not to use cookies. So far, this is the only thing I've got (this is my first attempt with sessions).
<?php
session_start();
$token = $_POST['token'];
?>
When the user is logged in, I need them to be sent to overview.php. Should this be done from the login.php file when there's a token, or should it be done from index.php instead of heading to login.php?
Also, how do I check if there's an active session on the pages the user visits? I need the token on every page to make new calls to the JSON interface.
Lastly, I when the user needs to log out, I guess I would just head them to logout.php or something like that and end the session there?
Thank you so much for your help!