johnman Posted September 10, 2021 Share Posted September 10, 2021 Good day I have written a php code that allows users to login to a dashboard successfully and works fine. Right now i want the code to return and display response on the next page i.e dashboard when logged into dashboard see the code below $data = array( 'EmailAddress' => '[email protected]', 'Password' => '1234567890', ); $url = "https://website.com/Login.php"; $nextPage = "dashboard.php"; $options = array( 'http' => array( 'method' => 'POST', 'content' => json_encode( $data ), 'header'=> "Content-Type: application/json\r\n" . "Accept: application/json\r\n" ) ); $context = stream_context_create( $options ); $result = file_get_contents( $url, false, $context ); $response = json_decode( $result, true ); //echo $result; to see the result remove comment and add comment to header('Location: '.$nextPage); if ($response["Message"] !== "The request is invalid." && $response["Authenticated"]) { echo "login successfull"; header('Location: '.$nextPage); } else { echo "login failed!"; } Kindly help Quote Link to comment https://forums.phpfreaks.com/topic/313695-php-curl-login-with-session-and-display-response-on-next-page/ Share on other sites More sharing options...
gw1500se Posted September 10, 2021 Share Posted September 10, 2021 First please edit your post and use the code icon (<>) at the top of the menu and specify PHP. Second, what did you try and what did not work as expected or that error do you get? Quote Link to comment https://forums.phpfreaks.com/topic/313695-php-curl-login-with-session-and-display-response-on-next-page/#findComment-1589787 Share on other sites More sharing options...
gizmola Posted September 10, 2021 Share Posted September 10, 2021 This doesn't have anything to do with curl, really. Your question is: "How can I pass information between pages?" This is both a function of HTTP protocol itself (GET requests, POST requests, cookies) and PHP's support for those things ($_GET, $_POST, $_COOKIE, PHP Sessions). The simplest way would be to have dashboard.php accept a url parameter like msg. $msg = empty($_GET['msg']) ? false : $_GET['msg']; if ($msg) { // Display the message in the dashboard } Then your login success code will be something like this: if ($response["Message"] !== "The request is invalid." && $response["Authenticated"]) { $msg = 'login successful'; header('Location: ' . $nextpage . '?msg=' . urlencode($msg)); } else { echo "login failed!"; } As I mentioned you can also do this using php sessions, or set a cookie. There are some advantages to each, and probably for an authentication scheme like this, you would want to use php sessions, since the problem with using get parameters is that your dashboard currently has no way of knowing that a user has logged in or not. So more likely what you want is something like this: Start a session at the top of your login script: session_start(); /// other code if ($response["Message"] !== "The request is invalid." && $response["Authenticated"]) { $_SESSION['login'] = true; $_SESSION['msg'] = 'login successful'; header('Location: ' . $nextpage); } else { echo "login failed!"; } Then for Dashboard: session_start(); if (empty($_SESSION['login']) || $_SESSION['login'] == false) { header('Location: /login.php'); exit; } $msg = empty($_SESSION['msg']) ? false : $_SESSION['msg']; if ($msg) { // Display the message in the dashboard } Quote Link to comment https://forums.phpfreaks.com/topic/313695-php-curl-login-with-session-and-display-response-on-next-page/#findComment-1589812 Share on other sites More sharing options...
johnman Posted September 11, 2021 Author Share Posted September 11, 2021 gizmola thank you for your response i will try it 1 Quote Link to comment https://forums.phpfreaks.com/topic/313695-php-curl-login-with-session-and-display-response-on-next-page/#findComment-1589828 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.