-1

I am new to JSON data transfer. I want to make a user click on a link in a webpage and that should redirect the user to another page with his login credentials in the url and display it there. Now this all I want to send and receive through JSON . I am working on PHP environment. I am adding a short code on which I am working but not knowing how to proceed exactly.

send.php

<?php

$data = '{ "user" : [   
                 { "email" : "xyz@gmail.com",  
                   "password"  : "xyz@123",
                   "employee_id"       : 77

                 }
               ]
    } ';

$url_send ="http://localhost/cwmsbi/recieve.php";
$str_data = json_encode($data);

function sendPostData($url_send, $post){
 $ch = curl_init($url);
  curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");  
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  curl_setopt($ch, CURLOPT_POSTFIELDS,$post);
  curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); 
  curl_setopt($ch, CURLOPT_HTTPHEADER, array(                                                                          
    'Content-Type: application/json',                                                                                
    'Content-Length: ' . strlen($post))                                                                       
   );  
  $result = curl_exec($ch);
  curl_close($ch);  // Seems like good practice
  return $result;
}

echo " " . sendPostData($url_send, $str_data);

?>

And receive.php

<?php

$json_input_data=json_decode(file_get_contents('php://input'),TRUE);

print_r( $json_input_data);


?>

Now when I am running send.php on my localhost, it displays the data on same page but does not goes to recieve.php. How this can be achieved? I am curious and in need of this too. How can I run a JSON file and where should i obtain results? Your guidance will be immensely useful to me right now.

Ansh
  • 269
  • 2
  • 15

1 Answers1

0

First of all i see you are json encoding $data two times (as when it gets defines it is already a json string and then you do $str_data = json_encode($data);).

If you want to achive the change of location with post data too, you can't use curl (POST data and redirect user by PHP CURL - read this question for further infos) - and i don't think you can do it by php only.

If i was trying to achive what you're trying to achive (and i would never make a page to show login password to users - as it is bad practice to show a password, even in emails), i suggest to set the json string into $_SESSION variable in send.php and redirect with header("Location: http://localhost/cwmsbi/recieve.php") where you get the json data from $_SESSION variable and you print it.

I did not make an example as i think this one perfectly suites you: https://stackoverflow.com/a/42215249/9606459

Extra hint: even if placing the password in php $_SESSION variable is better than put it in post request, remember you are doing bad practice and at least remember to empty out that json string in $_SESSION variable after you print it.

e.g.:

unset($_SESSION['user_data']);
Giacomo Penuti
  • 1,028
  • 11
  • 14
  • Thanks for such detailed description. Abiding by your sayings, I want to know few things first. 1. If i will be using `session` then why do I need to use JSON? 2. I actually want to take credentials from a .net project and then process it on PHP project. Is it possible? Thanks again – Ansh Aug 13 '18 at 10:44
  • If my answer suits your question, please mark as accepted. 1. I kept the JSON data encoding cause it was your requirment to use JSON encoding. Of course you can also directly set such variables directly in $_SESSION array using one element with proper key for each. 2. Since that was not part of the question my answer is intended as a solution for the question asked (where we got php files and the same server of course). Of course it is possible to sync or import credentials from different platforms, if you want help about that edit your question or make a new one with appropriate examples. – Giacomo Penuti Aug 13 '18 at 12:37