简体   繁体   English

PHP获取URL重定向

[英]PHP Get to URL redirect

Hey i have the following issues / thinking problem 嘿,我有以下问题/思考问题

<?php
$fname=$_GET['buyer_first_name'];
$lname=$_GET['buyer_last_name'];
$email=$_GET['buyer_email'];
$orderid=$_GET['order_id']

?>

This parameters will coming in by GET and after they get in / i would love an automatic redirect to the following URL with the above parameters in it 此参数将由GET传入,输入后/我希望自动重定向到带有上述参数的以下URL

www.mydomain.com/query.php?k=test&action=add&r=$orderid&n=$email www.mydomain.com/query.php?k=test&action=add&r=$orderid&n=$email

How can i do that? 我怎样才能做到这一点? My main problem is how i can set the different parameters in the url.. 我的主要问题是如何在URL中设置不同的参数。

You can first check the $_SERVER['REQUEST_METHOD'] if it is a 'GET' and check if all your expected parameters are set. 您可以先检查$_SERVER['REQUEST_METHOD']是否为'GET',然后检查是否设置了所有期望的参数。 Then you can use http_build_query to build your url and use header to go to your url. 然后,您可以使用http_build_query构建您的网址,并使用标头转到您的网址。

if (
    $_SERVER['REQUEST_METHOD'] === 'GET' &&
    isset($_GET['buyer_first_name']) &&
    isset($_GET['buyer_last_name']) &&
    isset($_GET['buyer_email']) &&
    $_GET['order_id']
) {
    $params = array(
        'k' => 'test',
        'action' => 'add',
        'r' => $_GET['order_id'],
        'n' => $_GET['buyer_email']
    );

    $url = 'www.mydomain.com/query.php?' .  http_build_query($params);
    header("location:" . $url);
}
header("location: www.mydomain.com/query.php?k=test&action=add&r=$orderid&n=$email");

如果您使用header('location ...,请确保在页面上看不到任何内容,否则将收到“已发送标题,输出开始...。”错误。

This should pretty much do what you are trying to accomplish 这几乎可以完成您要完成的任务

<?php
    if( $_SERVER['REQUEST_METHOD']=='GET' && isset( $_GET['buyer_first_name'],$_GET['buyer_last_name'],$_GET['buyer_email'],$_GET['order_id'] ) ){

        header( "location: www.mydomain.com/query.php?k=test&action=add&r={$_GET['order_id']}&n={$_GET['buyer_email']}" );

    }
?>

http_build_query is a cool function http_build_query是一个很酷的功能

$parameters = array(
  'k'      => 'test',
  'action' => 'add',
  'r'      => $orderid,
  'n'      => $email,
);

$target = 'www.mydomain.com/query.php';
$url = $target.'?'.http_build_query($parameters);
header("location: $url");

You can use as below : 您可以使用以下方法:

<?php
$fname='';$lname='';$email='';$orderid='';
if(isset($_GET['buyer_first_name']))
    $fname=stripslashes($_GET['buyer_first_name']);
if(isset($_GET['buyer_last_name']))
    $lname=stripslashes($_GET['buyer_last_name']);
if(isset($_GET['buyer_email']))
    $email=stripslashes($_GET['buyer_email']);
if(isset($_GET['order_id']))
    $orderid=stripslashes($_GET['order_id']);
$fullurl = "www.mydomain.com/query.php?k=test&action=add&r=".$orderid."&n=".$email;
header("location:".$fullurl);
?>
$querystring = "www.websitedomain.com/redirect.php?".$_SERVER['QUERY_STRING'];
header('location:'.$querystring);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM