Jump to content

php curl login with session and display response on next page


johnman

Recommended Posts

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' => 'john@mail.com',

        '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

Link to comment
Share on other sites

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
}

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.