如何使用html按钮在不丢失会话的情况下更改页面


How to use html button to change page without losing session

我是php的新手,我想我正在尝试做一些简单的事情。我有一个非常简单的Web应用程序,用户通过第三方(instagram)登录并进行身份验证,并显示一个欢迎页面。当用户尝试导航到从欢迎页面链接的另一个页面时,会出现我的问题。当单击链接并加载新页面时,会话变量似乎没有传递。

您可以在下面看到欢迎屏幕 php 代码。

<?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';

?>

这是将欢迎页面链接到应用中另一个页面的链接之一的示例 <p><a href="map.php">Map</a></p>

这是地图的 php 代码.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!';
?>

当用户将页面更改为映射时.php会话变量将丢失。我该如何纠正?

$_Session不是$_SESSION。你需要称呼它为正确。

不工作示例:(即使在同一页面上)

<?php
session_start();
$_SESSION['d'] = 'a';
echo $_Session['d'];

工作:

<?php
session_start();
$_SESSION['d'] = 'a';
echo $_SESSION['d'];

$_SESSION[]是PHP中的一个global variable,所以你不能那样使用它$_session[]

在PHP中,全局变量是那些可以在所有php文件中访问的变量,php定义了一些可用于所有php脚本的全局变量。例如 $_POST , $_SESSION , $_REQUEST.

因此,请使用 $_SESSION['d'] 而不是 $_session['d']。我希望它对你有用。

只需使用 $_SESSION['loggedIn'];

而不是 $_Session['loggedIn'];

相关文章: