简体   繁体   中英

How to use html button to change page without losing session

I'm new to php and I am trying to do something simple I think. I have a very simple web application where users log in and authenticate via a third-party (instagram) and are displayed a welcome page. My issues arises when users try to navigate to another page linked from the welcome page. The session variables don't seem to be passed when the click on a link and a new page is loaded.

You can see the welcome screen php code below.

<?php
/**
* Instagram PHP API
*
* @link https://github.com/cosenary/Instagram-PHP-API
* @author Christian Metz
* @since 01.10.2013
*/

session_start();

echo 'var dump before the login in command </br>';
var_dump($_SESSION['loggedIn']);

require_once 'Instagram.php';
use MetzWeb\Instagram\Instagram;


// initialize class
$instagram = new Instagram(array(
'apiKey'      => 'API KEY',
'apiSecret'   => 'API SECRET',
'apiCallback' => 'CALLBACK' // must point to success.php
 ));

// receive OAuth code parameter
$code = $_GET['code'];


//var_dump($instagram);

//echo '<br>This is the code from Instagram: ' .  $code; Testing my query string grabbing code

// check whether the user has granted access
if (isset($code)) {

//echo '<br> I made it inside the if statement </br>'; Testing if the variable code is not null

// receive OAuth token object
 $data = $instagram->getOAuthToken($code);
 //echo 'I requested the Auth Token! Data vardump below </br>';
 //var_dump($data);
 $username = $username = $data->user->username;
 //echo '<br> I got the user data! vardump below </br>';
 //var_dump($username);


// store user access token
$instagram->setAccessToken($data);


// now you have access to all authenticated user methods
$result = $instagram->getUserMedia();

$_SESSION['instagramClassFromLogin'] = $instagram;
$_SESSION['loggedIn']= true;
//echo 'var dump after the login in command </br>';
//var_dump($_SESSION['loggedIn']);


} else {

// check whether an error occurred
if (isset($_GET['error'])) {
echo 'An error occurred: ' . $_GET['error_description'];
}

}

include 'scripts/LoginCheck.php';


?>

This is an example of one of the links that links the welcome page to another page in the app <p><a href="map.php">Map</a></p>

This is the php code for map.php

<?php

/**
* Instagram PHP API
*
* @link https://github.com/cosenary/Instagram-PHP-API
* @author Christian Metz
* @since 01.10.2013
*/

session_start();

require_once 'Instagram.php';
use MetzWeb\Instagram\Instagram;

echo 'Running php code!';
echo '<br> Var dumping logged in variable </br>';
var_dump($_Session['loggedIn']);

$instagramClass = $_Session['instagramClassFromLogin'];
echo '<br> grabbed the instagram class!';

$username = $instagramClass->user->username;

//include 'scripts/LoginCheck.php';
echo '<br> passed the login check!';

?>

When the user changes the page to map.php the session variables are lost. How can I rectify that?

$_Session is not $_SESSION . You need to call it right.

Not working example: ( even on the same page )

<?php
session_start();

$_SESSION['d'] = 'a';
echo $_Session['d'];

Working :

<?php
session_start();

$_SESSION['d'] = 'a';
echo $_SESSION['d'];

$_SESSION[] is a global variable in PHP, So you can't use it like that $_session[]

In PHP global variables are those variables that are accessible inside your all php file and php defines some of the global variables which are available to all php scripts. Ex - $_POST , $_SESSION , $_REQUEST.

So, use $_SESSION['d'] instead of $_session['d'] . I hope it will work fine for you.

Just use $_SESSION['loggedIn']; instead of $_Session['loggedIn'];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM