将$ _GET变为变量或放入URL

时间:2016-04-22 23:24:03

标签: php

如何放置$ _GET [' ****'];成为一个字符串或使其成为一个变量。 例如,我有这个网址:

http://localhost/PhpProject2/product_page.php?rest_id=3/area=Enfield

我想从网址获取区域和rest_id。为了将另一个页面重定向到这个确切的页面。

echo"<script>window.open('product_page.php?rest_id= 'put get here'/area='put get here'','_self')</script>";

我到目前为止做到了这一点:

if(isset($_GET['rest_id'])){
if(isset($_GET['rest_city'])){

 $_GET['rest_id'] = $rest_id;
 }
 }

这显然不起作用,所以我的问题是如何将2 $ _GET转换为变量或将$ _GET调用到重定向字符串中。

到目前为止我累了

echo"<script>window.open('product_page.php?rest_id=' . $GET['rest_id'] . '/area='put get here'','_self')</script>";

最佳做法如何或是什么?

1 个答案:

答案 0 :(得分:1)

好的,首先要做的事情。在您的网址中,您必须使用和号“&amp;”分隔参数,例如

http://localhost/PhpProject2/product_page.php?rest_id=3&area=Enfield

此外,您必须将$ _GET值分配给变量,而不是相反,例如

$rest_id = $_GET['rest_id'];

所以如果你创建一个名为product_page.php的PHP文件并使用我给你的url,你的PHP代码看起来像这样,它应该可以工作..

<?php
if (isset($_GET['rest_id'])){
    $rest_id = $_GET['rest_id'];
}

if (isset($_GET['rest_id'])){
    $area = $_GET['area'];
}

$url = 'other_page.php?rest_id=' . $rest_id . '&area=' . $area;
header("Location: $url");
?>

这里的问题是你为什么要从这个页面重定向到另一个,而不是直接将参数发送到“other_page.php”????