<?php
session_start();
include_once $_SERVER['DOCUMENT_ROOT'].'/settings/ReadIni.php';
include_once $_SERVER['DOCUMENT_ROOT'].getinivalue('Paths', 'database_connect_location');
if( $_SERVER['REQUEST_METHOD'] == "POST" ) {
if( $dberror == "" ) {
if(isset($_SESSION["uid"])) {
$json = array("status" => getinivalue('ReturnValues', 'request_failure'), "message" => "Error: ".getinivalue('ReturnValues', 'session_already_running'));
}
else
{
$indata = file_get_contents('php://input');
$indata = json_decode($indata);
$password = $indata->pass;
$loginid = mysqli_real_escape_string($conn, $indata->loginid);
$pass = mysqli_real_escape_string($conn, $password);
if(($loginid != "") && ($pass != "")) {
$qrymailchk = "SELECT * from user_master where user_mail='$loginid'";
$qryphonechk = "SELECT * from user_master where user_phone='$loginid'";
$resmailchk = mysqli_query($conn, $qrymailchk);
$resphonechk = mysqli_query($conn, $qryphonechk);
$row1 = mysqli_fetch_array($resmailchk, MYSQLI_BOTH);
$row2 = mysqli_fetch_array($resphonechk, MYSQLI_BOTH);
if($row1 || $row2) {
$dbpass = ($row1) ? $row1['user_pass'] : $row2['user_pass'];
if ($pass == $dbpass) {
/*$passchk = password_verify($pass, $dbpass);*/
$_SESSION["uid"] = ($row1) ? $row1['user_code'] : $row2['user_code'];
$_SESSION["un"] = ($row1) ? $row1['user_name'] : $row2['user_name'];
$_SESSION["em"] = ($row1) ? $row1['user_mail'] : $row2['user_mail'];
$_SESSION["ph"] = ($row1) ? $row1['user_phone'] : $row2['user_phone'];
$words = explode(" ",$_SESSION["un"]);
$_SESSION["fn"] = $words[0];
$json = array("status" => getinivalue('ReturnValues', 'request_success'), "UserName" => $_SESSION["un"], "UID" => $_SESSION["uid"]);
// $URL = "/services.php";
// echo '<META HTTP-EQUIV="refresh" content="0;URL=' . $URL . '">';
// echo "<script type='text/javascript'>document.location.href='{$URL}';</script>";
//
// exit();
}
else {
$json = array("status" => getinivalue('ReturnValues', 'request_failure'), "message" => "Error: ".getinivalue('ReturnValues', 'invalid_credentials'));
mysqli_close($conn);
}
}
else{
$json = array("status" => getinivalue('ReturnValues', 'request_failure'), "message" => "Error: ".getinivalue('ReturnValues', 'invalid_credentials'));
mysqli_close($conn);
}
}
}
}
else {
$json = array("status" => getinivalue('ReturnValues', 'request_failure'), "message" => "Error: ".$dberror);
}
}
else {
$json = array("status" => getinivalue('ReturnValues', 'request_failure'), "message" => "Error: ".getinivalue('ReturnValues', 'invalid_request_type'), "Required" => getinivalue('ReturnValues', 'request_type_post'));
}
header('Content-type: application/json');
echo json_encode($json);
?>
[Contents-GET]<?php
session_start();
include_once $_SERVER['DOCUMENT_ROOT'].'/settings/ReadIni.php';
include_once $_SERVER['DOCUMENT_ROOT'].getinivalue('Paths', 'database_connect_location');
if( $_SERVER['REQUEST_METHOD'] == "GET" ) {
if( $dberror == "" ) {
if(isset($_GET['uid'])) {
$uid = $_GET['uid'];
if(isset($_SESSION["uid"])) {
if($_SESSION["uid"] == $_GET['uid']) {
$qry1 = "SELECT device_code from user_device where user_code='".$uid."' and device_active='1'";
$res1 = mysqli_query($conn, $qry1);
$json = array("status" => getinivalue('ReturnValues', 'request_success'), "list_of_devices" => NULL);
if(mysqli_num_rows($res1)) {
$device_list = array();
while ($devices = mysqli_fetch_array($res1, MYSQLI_BOTH)) {
$qry2 = "SELECT device_name from device_master where device_code='".$devices[0]."'";
$res2 = mysqli_query($conn, $qry2);
$row = mysqli_fetch_array($res2, MYSQLI_BOTH);
$device_detail = array("device_code" => $devices[0], "device_name" => $row['device_name']);
array_push($device_list, $device_detail);
}
$json["list_of_devices"] = $device_list;
}
}
else {
$json = array("status" => getinivalue('ReturnValues', 'request_failure'), "message" => getinivalue('ReturnValues', 'invalid_userid'));
}
}
else {
$json = array("status" => getinivalue('ReturnValues', 'request_failure'), "message" => getinivalue('ReturnValues', 'no_session'));
}
}
else {
$json = array("status" => getinivalue('ReturnValues', 'request_failure'), "message" => getinivalue('ReturnValues', 'input_not_set'));
}
}
else {
$json = array("status" => getinivalue('ReturnValues', 'request_failure'), "message" => "Error: ".$dberror);
}
}
else {
$json = array("status" => getinivalue('ReturnValues', 'request_failure'), "message" => "Error: ".getinivalue('ReturnValues', 'invalid_request_type'), "Required" => getinivalue('ReturnValues', 'request_type_get'));
}
header('Content-type: application/json');
echo json_encode($json);
?>
ASKER
ASKER
ASKER
ASKER
Don't bother with the POST login part of the process. Instead use an API key that is part of the GET request.
So when I log into a session from the site(domain.com) interface it returns a cookie but when I call the api(www.domain.com)...PHP session cookies, by default, are not set for the entire domain. They are only set for the subdomain. So a session that exists on test.domain.com will not collide with a session set on domain.com or sub.domain.com. In the article about sessions, look for "RAY_session_cookie_domain
ASKER
ASKER
what if don't want all subdomains to read the same cookie...Then you would have to write some programming to intervene. You could set the cookie for .domain.com (note the leading dot) and this will let the cookie serve domain.com and anysubdomain.domain.com. Then you can check the HTTP_HOST value and disallow anything other than domain.com and www.domain.com.
$json = array('SESSION' => print_r($_SESSION, true), 'COOKIES' => print_r($_COOKIE, true), 'POST' => print_r($_POST, true), 'GET' => print_r($_GET, true))
ASKER
json response for no running session ...That may happen if his android app does not act like a well-behaved browser, accepting and returning cookies, running JavaScript, and following redirects. The whole concept of the PHP session came into being when the only thing using HTTP was the browser/server relationship. There were no mobile devices, and the entire concept of mobile web access came about independently of browser-based web access. So while the browsers were doing the "right thing" with HTTP cookies, the mobile devices were not necessarily in touch with the way PHP sessions worked. I think the approach described in the article, where your API handshake includes the API key on every HTTP request, will be a good way forward.
ASKER
PHP is a widely-used server-side scripting language especially suited for web development, powering tens of millions of sites from Facebook to personal WordPress blogs. PHP is often paired with the MySQL relational database, but includes support for most other mainstream databases. By utilizing different Server APIs, PHP can work on many different web servers as a server-side scripting language.
TRUSTED BY
https://www.experts-exchange.com/articles/11909/PHP-Sessions-Simpler-Than-You-May-Think.html