amar ghodke amar ghodke - 1 year ago 99
PHP Question

php decode json object for login does not work properly

i am trying for json response to be integrating to frontend UI of e-commerce template.i am integrating json response for login to UI. i have done so far is getting value from json response of login. when i try to login with email values [email protected] and password 123456 it redirects to home page as give in javascript but even i try with incorrect unregistered password it redirect to home with incorrect values.

what is expected is when user login with above username and password then it redirect to home and welcome username to be shown at home page also logout label to be shown near to it.

Json response

{
"error": false,
"data": {
"userId": "65",
"name": "Name",
"email": "[email protected]",
"password": null,
"gender": "m",
"phone": "1234567890",
"dob": "1499797800000",
"bloodGroup": "A+",
"startDate": "1484159400000",
"fb_id": "",
"google_id": "",
"api_key": "3f009d72559f51e7e454b16e5d0687a1",
"otp": null,
"createdOn": "1497764349267",
"updatedOn": "1500979353326"
},
"message": "Login Successful.."
}


php

<?php include("header.php");?>
<div role="main" class="main">
<section class="form-section">
<div class="container">
<h1 class="h2 heading-primary font-weight-normal mb-md mt-lg">Login or Create an Account</h1>
<div class="featured-box featured-box-primary featured-box-flat featured-box-text-left mt-md">
<div class="box-content">
<!-- <form action="#"> -->
<div class="row">
<div class="col-sm-6">
<div class="form-content">
<h3 class="heading-text-color font-weight-normal">New Customers</h3>
<p>By creating an account with our store, you will be able to move through the checkout process faster, store multiple shipping addresses, view and track your orders in your account and more.</p>
</div>
<div class="form-action clearfix">
<a href="demo-shop-5-register.php" class="btn btn-primary">Create an Account</a>
</div>
</div>
<div class="col-sm-6">
<?php $json_string = '{"error":false,"data":{"userId":"65","name":"Name","email":"[email protected]","password":null,"gender":"m","phone":"1234567890","dob":"1499797800000","bloodGroup":"A+","startDate":"1484159400000","fb_id":"","google_id":"","api_key":"3f009d72559f51e7e454b16e5d0687a1","otp":null,"picturePath":"\/var\/www\/html\/JacchaBacchaDevelopers\/v1\/uploads\/1499924350_pp.png","createdOn":"1497764349267","updatedOn":"1500979353326"},"message":"Login Successful.."}';
$array = json_decode($json_string, true);
//$message = "wrong answer";
if(isset($_POST['login']))
{
$user = $array['data']['email'];
$pass = $array['data']['password'];
if(is_array($array)) {
$_SESSION["userId"] = $array['data']['userId'];
}

if($user===$array['data']['email'] && $pass===$array['data']['password'])
{
$_SESSION["userId"] = $array['data']['userId'];
echo '<script language="javascript">';
echo "window.location.href = 'demo-shop-5.php'";
echo '</script>';
}
else
{
echo '<script language="javascript">';
echo 'alert("invalid email or password")';
echo '</script>';
}
}?>
-->
<form method="POST" action="">
<div class="error-message"><?php if(isset($message)) { echo $message; } ?></div>
<div class="form-content">
<h3 class="heading-text-color font-weight-normal">Registered Customers</h3>
<p>If you have an account with us, please log in.</p>
<div class="form-group">
<label class="font-weight-normal">Email Address <span class="required">*</span></label>
<input type="email" class="form-control" required name="email" value="<?php echo $email;?>">
</div>

<div class="form-group">
<label class="font-weight-normal">Password <span class="required">*</span></label>
<input type="password" class="form-control" required name="password" value="<?php echo $password;?>">
</div>
<p class="required">* Required Fields</p>
</div>
<div class="form-action clearfix">
<a href="#" class="pull-left">Forgot Your Password?</a>
<input type="submit" class="btn btn-primary" name="login" value="Submit">
</div>
</form>
</div>
</div>
<!-- </form> -->
</div>
</div>
</div>
</section>
</div>
<?php include("footer.php"); ?>


base url for webservice

http://139.59.36.134/JacchaBacchaDevelopers/v1/index.php/login

Answer Source

You have to store $_POST value in email and pass. chaange following line

$user = $array['data']['email'];
$pass = $array['data']['password'];  

to

$user = $_POST['email'];
$pass = $_POST['password']; 
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download