【问题标题】:Redirect a URL with Post data使用 Post 数据重定向 URL
【发布时间】:2014-07-03 16:25:58
【问题描述】:

我想使用一些 POST 数据将用户从 page1 重定向到 page2。Page1 和 page2 位于 两个不同的域上,我可以控制 两者 p>

第 1 页

<?php
$chars="stackoverflowrules"
?>

我想将字符作为帖子数据提交并重定向到第 2 页。

然后一页 2 我想使用 POST 数据,例如

<?php
$token = $_POST['chars'];
echo $token;
?>

【问题讨论】:

    标签: php post


    【解决方案1】:

    我是用表单和 JS 做的

    在第 1 页

    <?php
    $chars="stackoverflowrules";
    ?>
    <html>
    <form name='redirect' action='page2.php' method='POST'>
    <input type='hidden' name='chars' value='<?php echo $chars; ?>'>
    <input type='submit' value='Proceed'>
    </form>
    <script type='text/javascript'>
    document.redirect.submit();
    </script>
    </html>
    

    在第 2 页

    <?php
    $token = $_POST['chars'];
    echo $token;
    ?>
    

    【讨论】:

      【解决方案2】:
      1. 在第 1 页,使用curl 将数据发布到第 2 页。
      2. 在那里,将 POST 后的数据存储在某处(数据库?)。
      3. 从第 1 页重定向到第 2 页
      4. 已取回数据。

      【讨论】:

        【解决方案3】:

        您想为此使用curl()

        在 page1.php 上,执行以下操作:

        $data = $_POST; 
        
        // Create a curl handle to domain 2
        $ch = curl_init('http://www.domain2.com/page2.php'); 
        
        //configure a POST request with some options
        curl_setopt($ch, CURLOPT_POST, true);
        
        //put data to send
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);  
        
        //this option avoid retrieving HTTP response headers in answer
        curl_setopt($ch, CURLOPT_HEADER, 0);
        
        //we want to get result as a string
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        
        //execute request
        $result = curl_exec($ch);
        
        // now redirect to domain 2
        header("Location: http://domain2.com/page2.php");
        

        在第 2 页,您可以检索 POST 数据:

        <?php
        
        $token = $_POST['secure_token'];
        echo $token;
        
        ?>
        

        【讨论】:

        • 这不会按预期工作,因为现在有 2 个请求到第 2 页。一个是来自 curl 的 POST。其次是来自header() 的GET。您试图从后者获取 $_POST,但这是行不通的。
        猜你喜欢
        • 2011-09-30
        • 2017-07-02
        • 2014-01-31
        • 2016-02-04
        • 1970-01-01
        • 2021-01-09
        • 1970-01-01
        • 2011-07-31
        相关资源
        最近更新 更多