How to use session with angularJS and PHP backend -
i'm developing angularjs application php backend, implemented authentication step, , i'm trying display data of authenticated user in view after authentication.
i used session token display user data in other view if did read in tutorials sessions , tokens more 2 weeks, still can't display user data. , error in display.php file:
undefined index: token
how can manage problem?
login.php
<?php session_start(); $data = json_decode(file_get_contents("php://input")); $connect = mysqli_connect("localhost", "root", "", "database"); if(count($data) > 0) { $email=mysqli_real_escape_string($connect, $data->email); $password=mysqli_real_escape_string($connect, $data->password); $query = 'select * `client` (emailclient = "'.$email.'" , password= "'.$password.'")'; $q = mysqli_query($connect , $query); if(mysqli_num_rows($q) > 0 ) { $token = md5($email.time()."51395+81519851"); $query = "update client set token = '".$token."' emailclient = '".$email."'"; mysqli_query($connect , $query); $_session["logged_in"] = true; $_session["token"] = $token; $result['message'] ='logged in'; $result['email'] =$email; $result['token'] = $token; $resultstring=json_encode($result); $resultstring=str_replace("null", '""', $resultstring); echo $resultstring; exit; } $result['message'] ='the username or password incorrect!'; $resultstring = json_encode($result); $resultstring = str_replace("null",'""',$resultstring); echo $resultstring; exit; } ?>
display.php
<?php session_start(); $connect = mysqli_connect("localhost", "root", "", "database"); $output = array(); $query = "select name,adresse client token = '".$_session['token']."'"; $result = mysqli_query($connect, $query); if(mysqli_num_rows($result) > 0) { while($row = mysqli_fetch_array($result)) { $output[] = $row; } echo json_encode($output); } ?>
you trying access content of "token" in $_session, must add check beforehand ensure "token" in array named $_session. encountered error because session not hold "token" data (a var_dump / print_r on $_session should confirm that).
add following:
if (empty($_session['token'])) { // output error message, or take specific action handle case. return; }
Comments
Post a Comment