简体   繁体   English

PHP使用POST数据重定向到另一个页面

[英]Php redirect to another page with POST data

In my php application I just need to redirect the user to a specific url (a third party) with some POST data to get their service done. 在我的php应用程序中,我只需要使用一些POST数据将用户重定向到特定的URL(第三方)即可完成服务。

The data should pass to that Url securely (As they are the login credentials to that third party system). 数据应安全地传递到该网址(因为它们是该第三方系统的登录凭据)。

I tried to do this with cURL but it doesn't do any redirect to that specific page. 我尝试使用cURL来执行此操作,但它不执行任何重定向到该特定页面的操作。 It works fine if I post the form with hidden fields with the values set up. 如果我发布带有设置了隐藏值的表单的表单,则效果很好。 But anyone can see the posted data easilly by viewing "View page source" option in browser. 但是任何人都可以通过在浏览器中查看“查看页面源代码”选项来轻松查看发布的数据。

I am searching for a sloution for long time but still no luck. 我正在寻找一个长期的骗子,但仍然没有运气。 If someone can guide me would be really grateful. 如果有人可以指导我,将不胜感激。 Thanks in advance...!!!! 提前致谢...!!!!

Edits 编辑

I can not use session as this is a third party url and it asks to post the login credentials to enter to that page. 我不能使用会话,因为这是第三方URL,它要求发布登录凭据以进入该页面。

Functionality in breif, breif中的功能,

What I have to do is all the user credantials are stored here in my system. 我要做的是将所有用户凭据存储在我的系统中。
When user press a button it should redirect him to that url and should POST that credetials to enter to that external 3rd party page. 当用户按下按钮时,应将他重定向至该URL,并应发布该凭证以输入该外部第3方页面。 At the time when user press the button he doen't have to enter credantials by a text box. 在用户按下按钮时,他不必通过文本框输入证书。 Thats why I even can't hard code hiddenfield as its visible if go to "View page source" option in browser. 这就是为什么如果在浏览器中转到“查看页面源代码”选项,我甚至都无法将hiddenfield硬编码为可见的原因。

---------- ----------

Simply I am looking for a method (in php) which will redirect the user to a url while POSTING some data. 我只是在寻找一种方法(在php中),该方法将在发布一些数据时将用户重定向到url。 It can be simply done by creating a FORM and having textinputs(Textboxes) to allow the user to enter the post information and submit. 可以简单地通过创建一个FORM并使用textinputs(Textboxes)来允许用户输入帖子信息并提交来完成。

But in my case I can not let the user to enter the POST information for textboxes and submit. 但就我而言,我不能让用户输入文本框的POST信息并提交。 It has to POST the data and redirect to a specified URL when user click a button. 当用户单击按钮时,它必须发布数据并重定向到指定的URL。 Tried to do it via cURL but it does not redirect user to the URI. 尝试通过cURL进行此操作,但它不会将用户重定向到URI。 Also If I add hiddenFields hardcoding POST values it is not secured as anyone can see the POST information by Browser "View Page Source" option. 另外,如果我添加了hiddenFields硬编码的POST值,则它不安全,因为任何人都可以通过浏览器的“查看页面源”选项查看POST信息。

Just make a form with number of fields equal to credentials. 只需创建一个字段数等于凭据的表单即可。 ie if you want to send username and password, do like this, 例如,如果您要发送用户名和密码,请这样做,

<form id="uform" action="" method="post">
        <input type="text" name="uname" id="uname" style="opacity:0;" />
        <input type="text" name="pass" id="pass" style="opacity:0;" />
    </form>

In file.php, 在file.php中,

<php
var $_uname = "Required username";
var $_pass = "Required password";
echo $_uname.",".$_pass;
?>

Then in your javascript, In the button click event, Get this form and set the values of input fields. 然后在您的javascript中,在按钮单击事件中,获取此表单并设置输入字段的值。 Then submit the form, 然后提交表格,

<script>
var ajax = new XMLHttpRequest();
ajax.open("GET","file.php",true);
ajax.send();

ajaxRequest.onreadystatechange = function(){
    if(ajaxRequest.readyState == 4){
    var dt = ajax.responseText;
    dtsp = dt.split(",");
    var frm = document.getElementById("uform");
    var inp1 = document.getElementById("uname");
    var inp2 = document.getElementById("pass");
    inp1.value = dtsp[0];
    inp2.value = dtsp[1];
    frm.action = 'Url of the site to redirect to';
    frm.submit();
    }
}

    </script>

Then In the second site on which the user is redirected, if you want to get the data, do this, 然后,在要重定向用户的第二个站点中,如果要获取数据,请执行此操作,

<php
$uname = $_POST["uname"];

$pass = $_POST["pass"];
?>

And here u go. 然后你去。 You will have the data transferred from one site to other safely. 您将数据安全地从一个站点传输到另一站点。

If you need to send data associated with a user to a third party without giving that data to the user, then you need to use a process along these lines: 如果您需要将与用户关联的数据发送给第三方而又不将该数据提供给用户,则需要按照以下步骤使用流程:

  • Send the data using server side code 使用服务器端代码发送数据
  • Get a token to identify that set of data (you can include it in the data you send originally, or get it from the response from the third party site) 获取令牌以标识该组数据(您可以将其包含在原始发送的数据中,也可以从第三方站点的响应中获取)
  • Redirect the user to the third party site and include the token in the URL 将用户重定向到第三方站点,并将令牌包括在URL中

Obviously, this requires the co-operation of the third party site. 显然,这需要第三方站点的合作。

Update after a comment was added to the question: 向问题添加评论后更新:

This is a third party url and it asks to post the login credentials to enter to that page. 这是第三方URL,它要求发布登录凭据以进入该页面。 Therefore session is not going to work here. 因此,会话在这里不起作用。

It sounds like you are trying to let users login as you without giving them the credentials. 听起来您好像在尝试让用户在不提供凭据的情况下登录。 That is impossible. 那是不可能的。

function do_post_request($url, $data, $optional_headers = null)
{
  $params = array('http' => array(
              'method' => 'POST',
              'content' => $data
            ));
  if ($optional_headers !== null) {
    $params['http']['header'] = $optional_headers;
  }
  $ctx = stream_context_create($params);
  $fp = @fopen($url, 'rb', false, $ctx);
  if (!$fp) {
    throw new Exception("Problem with $url, $php_errormsg");
  }
  $response = @stream_get_contents($fp);
  if ($response === false) {
    throw new Exception("Problem reading data from $url, $php_errormsg");
  }
  return $response;
}

(you can also use ajax) ;) (您也可以使用ajax);)

You can send it through session , 您可以通过session发送它,

At 1st site do this, 在第一个站点上执行此操作

<?php
        session_start(); 
        $_dt = 'data to send';
        $_SESSION['dt'] = $_dt;
    ?>

In second do this, 第二步,

<?php
     session_start(); 
        $_dt = $_SESSION['dt']
?>

Data will be in $_dt . 数据将在$_dt

Have you considered either: 您是否考虑过:

a) encrypting it with javascript or a java app - which is how my bank used to do it.. a)使用javascript或Java应用程序对其进行加密-这是我的银行过去这样做的方式。

b) post the variables back to itself, encrypt them, assuming it encrypts, then bounce the page on to the new page with the encryted variables.. b)将变量发回到自身,对其进行加密(假设已加密),然后使用加密的变量将页面退回到新页面。

The only problem with encryption is you need to be able to unencrypt it to give it to the 3rd party app unless they accept specific encoding, in which case if you can unencrypt it, someone else will work out how. 加密的唯一问题是,除非他们接受特定的编码,否则您需要能够对其进行解密以将其提供给第三方应用程序,在这种情况下,如果您可以对其进行解密,那么其他人将可以解决该问题。

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

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