PHP.如何传递变量和自动重定向到另一个 PHP 文件 [英] PHP. How to pass variable and auto redirect to another PHP file
问题描述
我正在为 Facebook 开发应用程序.此处应检查用户打开应用程序时是否数据库中已存在用户.我想我会使用 $_SESSION
将 user's Id 传递给 checkIfExsists.php
I'm developing app for Facebook. Here when user open application should be checked If user already exists in database. I think I will use $_SESSION
to pass user's Id to checkIfExsists.php
所以我的 FacebookGetId.php 看起来像:
So my FacebookGetId.php looks like:
<?php
...
$id = $user_profile['id'];
$_SESSION['id'] = $id;
?>
所以 $id
现在是 '12345'
我只是不知道如何自动重定向到 checkIfExsists.php
检查该 ID 是否已存在于数据库中.
So $id
for now is i.e. '12345'
I just don't know how to make automatically redirect to checkIfExsists.php
to check If that Id already exsists in database.
它应该是这样的:当应用程序启动时,它应该获取用户的 ID 并自动重定向到 checkIfExsists.php
并传递该 ID.
It should be something like: When application is launched, It should take User's Id and automatically redirect to checkIfExsists.php
and pass that Id.
如果用户存在 checkIfExsists.php
应该将用户重定向到 application.php
,如果不存在 - 它应该重定向到 registration.php
If user exists checkIfExsists.php
should redirect user to application.php
, if not exists - It should redirect to registration.php
推荐答案
使用 header 函数
<?php
...
$id = $user_profile['id'];
$_SESSION['id'] = $id;
header('Location: checkIfExsists.php?id='.$id);
?>
在 checkIfExsists.php 上获取变量
on the checkIfExsists.php get the variable with
$id = $_GET["id"];
这会以您希望解决的方式解决您的问题,但是,这并不是它应该解决的方式,也许在 checkIfExists.php 内部应该是一个类而不是具有公共功能的结构化代码来检查存在 checkExistance
,所以你只需要:
That would solve your problem the way you want it to be solved, but, this isn´t neccesarilly the way it should be solved, maybe inside checkIfExists.php should be a class instead of structured code with a public function to check existance checkExistance
, so you will then just need:
include_once(checkIfExists.php);
$check = new checker();
$exists = $check->checkExistance($id) ;
这样你就不必在文件之间跳转,你可以有一个更好的方法来重用代码,问候.
this way you do not have to be jumping between files and you can have a better way to re-use code, regards.
这篇关于PHP.如何传递变量和自动重定向到另一个 PHP 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!