PHP。如何传递变量和自动重定向到另一个 PHP 文件

PHP. How to pass variable and auto redirect to another PHP file

我正在为 Facebook 开发应用程序。如果用户已存在于数据库中,则应检查用户打开应用程序时的位置。我想我会使用 $_SESSION 用户的 Id 传递给 checkIfExsists.php

所以我的 FacebookGetId.php 看起来像:

<?php
...
$id = $user_profile['id'];
$_SESSION['id'] = $id;
?>

所以 $id 现在是 '12345' 我只是不知道如何自动重定向到 checkIfExsists.php 以检查该 ID 是否已存在于数据库中。

它应该是这样的:启动应用程序时,它应该使用 用户的 Id 并自动重定向到 checkIfExsists.php 并传递该 Id。

如果用户存在 checkIfExsists.php 应该将用户重定向到 application.php,如果不存在 - 它应该重定向到 registration.php

使用header函数

<?php
...
$id = $user_profile['id'];
$_SESSION['id'] = $id;
header('Location: checkIfExsists.php?id='.$id);
?>

在checkIfExsists.php上用

获取变量
$id = $_GET["id"];

这会按照您希望的方式解决您的问题,但是,这不一定是解决问题的方式,也许 checkIfExists.php 中应该是 class 而不是具有 public 函数的结构化代码可检查是否存在 checkExistance,因此您只需要:

include_once(checkIfExists.php);
$check = new checker();
$exists = $check->checkExistance($id) ;

这样你就不必在文件之间跳转,你可以有更好的方法来重用代码, 问候。

使用header('Location:url_of_your_page.php?fbid='.$id)php header function

FacebookGetId.php

<?php
    ...
    $id = $user_profile['id'];
    $_SESSION['id'] = $id;
    header('Location:checkIfExsists.php?fbid='.$id)
?>

你可以通过 GET 和 header 来完成,例如 :)

header('Location: checkIfExists.php?userID=12345');

在checkIfExists.php

echo $_GET["userID"]

或在 checkIfExists.php 中,只需从 session 开始 sessions 加载 ID :)

使用这个:

<?php
 session_start();
 $id = $user_profile['id'];
 $_SESSION['id'] = $id;
 header('Location: checkIfExsists.php');
?>

然后在 checkIfExsists.php 页面上,检索变量:

<?php
 session_start();
 $id = $_SESSION['id'];
?>