php 将多个变量传递给 url 中的另一个页面

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13102489/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 04:46:31  来源:igfitidea点击:

Passing multiple variables to another page in url

phpurlsessionvariables

提问by zadubz

I'm passing a variable to another page in a url using sessions like this but it seems that I can't concatenate another variable to the same url and retrieve it in the next page successfully

我正在使用这样的会话将变量传递到 url 中的另一个页面,但似乎我无法将另一个变量连接到同一个 url 并在下一页中成功检索它

Page 1

第 1 页

session_start();
$event_id = $_SESSION['event_id'];
echo $event_id;

$url = "http://localhost/main.php?email=" . $email_address . $event_id;     

Page 2

第2页

if (isset($_GET['event_id'])) {
$event_id = $_GET['event_id'];}
echo $event_id;

echo $event_idshows an error Undefined variableon page 2 but if I use just the event_idin the $urllike here

echo $event_idUndefined variable在第 2 页上显示错误,但如果我event_id$url这里只使用类似的

 $url = "http://localhost/main.php?event_id=" . $event_id;

That works fine, but I need to be able to use both variables in the url so that page 2 can retrieve get them.

这工作正常,但我需要能够在 url 中使用这两个变量,以便第 2 页可以检索获取它们。

回答by JvdBerg

Use the ampersand &to glue variables together:

使用与号&将变量粘合在一起:

$url = "http://localhost/main.php?email=$email_address&event_id=$event_id";
//                               ^ start of vars      ^next var

回答by Asaph

Short answer:

简短的回答:

This is what you are trying to do but it poses some security and encoding problems so don't do it.

这是您正在尝试做的,但它会带来一些安全和编码问题,所以不要这样做。

$url = "http://localhost/main.php?email=" . $email_address . "&eventid=" . $event_id;

Long answer:

长答案:

All variables in querystrings need to be urlencoded to ensure proper transmission. You should never pass a user's personal information in a url because urls are very leaky. Urls end up in log files, browsing histories, referal headers, etc. The list goes on and on.

查询字符串中的所有变量都需要进行 urlencoded 以确保正确传输。您永远不应该在 url 中传递用户的个人信息,因为 url 非常容易泄露。Urls 最终会出现在日志文件、浏览历史记录、引用标题等中。这个列表还在继续。

As for proper url encoding, it can be achieved using either urlencode()or http_build_query(). Either one of these should work:

至于正确的 url 编码,可以使用urlencode()或来实现http_build_query()。这些中的任何一个都应该有效:

$url = "http://localhost/main.php?email=" . urlencode($email_address) . "&eventid=" . urlencode($event_id);

or

或者

$vars = array('email' => $email_address, 'event_id' => $event_id);
$querystring = http_build_query($vars);
$url = "http://localhost/main.php?" . $querystring;

Additionally, if $event_idis in your session, you don't actually need to pass it around in order to access it from different pages. Just call session_start()and it should be available.

此外,如果$event_id在您的会话中,您实际上不需要传递它以便从不同的页面访问它。只需致电session_start(),它应该可用。

回答by Crobzilla

Your first variable declartion must start with a ?while any additional must be concatenated with a &

您的第一个变量声明必须以?while开头,任何其他变量声明都必须与&

回答by Anthony Pfeiffer

Pretty simple but another said you dont pass session variables through the url bar 1.You dont need to because a session is passed throughout the whole website from header when you put in header file 2.security risks
Here is first page code

很简单,但另一个说你不通过 url 栏传递会话变量 1.你不需要,因为当你放入头文件时,会话从头文件传递到整个网站 2.安全风险
这是第一页代码

$url = "http://localhost/main.php?email=" . urlencode($email_address) . "&eventid=" . urlencode($event_id);

$url = "http://localhost/main.php?email=" . urlencode($email_address) . "&eventid=" . urlencode($event_id);

2nd page when getting the variables from the url bar is:

从 url 栏中获取变量时的第二页是:

if(isset($_GET['email']) && !empty($_GET['email']) AND isset($_GET['eventid']) && !empty($_GET['eventid'])){ ////do whatever here }

if(isset($_GET['email']) && !empty($_GET['email']) AND isset($_GET['eventid']) && !empty($_GET['eventid'])){ ////do whatever here }

Now if you want to do it the proper way of using the session you created then ignore my above code and call the session variables on the second page for instance create a session on the first page lets say for example:

现在,如果您想以正确的方式使用您创建的会话,请忽略我上面的代码并调用第二页上的会话变量,例如在第一页上创建一个会话,例如:

 $_SESSION['WEB_SES'] = $email_address . "^" . $event_id;

obvious that you would have already assigned values to the session variables in the code above, you can call the session name whatever you want to i just used the example web_ses , the second page all you need to do is start a session and see if the session is there and check the variables and do whatever with them, example:

很明显,您已经在上面的代码中为会话变量分配了值,您可以随意调用会话名称,我只是使用示例 web_ses ,第二页您需要做的就是启动会话并查看是否会话在那里并检查变量并对其执行任何操作,例如:

 session_start();
 if (isset($_SESSION['WEB_SES'])){
 $Array = explode("^", $_SESSION['WEB_SES']);
 $email = $Array[0];
 $event_id = $Array[1]
 echo "$email";
 echo "$event_id";
 }

Like I said before the good thing about sessions are they can be carried throughout the entire website if this type of code in put in the header file that gets called upon on all pages that load, you can simple use the variable wherever and whenever. Hope this helps :)

就像我之前说的,会话的好处是它们可以在整个网站中进行,如果这种类型的代码放在头文件中,在所有加载的页面上都会调用,您可以随时随地简单地使用该变量。希望这可以帮助 :)

回答by Vijayendran.R

<a href="deleteshare.php?did=<?php echo "$rowc[id]"; ?>&uid=<?php echo "$id";?>">DELETE</a>

Pass multiple Variable one page to another page

将多个变量一个页面传递到另一个页面

回答by Bhavyanshu

Use & for this. Using & you can put as many variables as you want!

为此使用 &。使用 & 您可以放置​​任意数量的变量!

$url = "http://localhost/main.php?event_id=".$event_id."&email=".$email;

回答by Mr. Alien

You are checking isset($_GET['event_id']but you've not set that get variable in your hyperlink, you are just adding email

您正在检查isset($_GET['event_id']但尚未在超链接中设置 get 变量,您只是添加email

http://localhost/main.php?email=" . $email_address . $event_id

http://localhost/main.php?email=" . $email_address . $event_id

And add another GET variable in your link

并在您的链接中添加另一个 GET 变量

$url = "http://localhost/main.php?email=$email_address&event_id=$event_id";

You did not use to concatenate your string if you are using "quotes

如果您使用"引号,则您没有使用连接字符串

回答by Jamal Azizbeigi

from php.net

来自 php.net

Warning The superglobals $_GETand $_REQUESTare already decoded. Using urldecode()on an element in $_GETor $_REQUESTcould have unexpected and dangerous results.

警告超全局变量$_GET$_REQUEST已经被解码。urldecode()$_GET或 中的元素上使用$_REQUEST可能会产生意外和危险的结果。

link: http://php.net/manual/en/function.urldecode.php

链接:http: //php.net/manual/en/function.urldecode.php

be careful.

当心。