简体   繁体   English

将标头设置为GET / POST请求

[英]Set headers to GET/POST request

We want to send authentication token and signature during GET/POST request. 我们希望在GET / POST请求期间发送身份验证令牌和签名。 We are not preferring to send security information through body Or parameter. 我们不希望通过body或parameter发送安全信息。 So we decided to send through headers in request. 因此,我们决定在请求中发送标头。

When we googled we got to know that we can do this using ajax post request. 当我们用谷歌搜索时,我们知道我们可以使用ajax发布请求来做到这一点。 But we want to move to next pages with headers. 但是我们想转到带有标题的下一页。

How we can implement this? 我们如何实现呢?

Edited: 编辑:

We had plan to store these information on cookies. 我们原计划将这些信息存储在cookie上。 But in iPhone if cookies is disabled will change behaviour of our website. 但是在iPhone中,如果Cookie被禁用,则会更改我们网站的行为。 So to overcome we are planning to send through headers. 因此,为了克服这一点,我们计划通过标头发送。

You can set X-AUTHENTICATION-TOKEN in ajax request something as below which I am using for a real time REST API call via ajax requests. 您可以在ajax请求中设置X-AUTHENTICATION-TOKEN ,如下所示,我正在通过ajax请求进行实时REST API调用。

$.ajax({
    type: "GET",
    beforeSend: function (xhr) {
        xhr.setRequestHeader('X-AUTHENTICATION-TOKEN', 'token string');
    },

The server side must set the header something as 服务器端必须将标头设置为

header('Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept, X_AUTHENTICATION_TOKEN');

And to retrieve the data on server side you may have 并在服务器端检索数据

if (isset($_SERVER['HTTP_X_AUTHENTICATION_TOKEN'])) {
    $request_header = $_SERVER['HTTP_X_AUTHENTICATION_TOKEN'];
} else {
    if (function_exists('getallheaders')) {
        foreach (getallheaders() as $header_name => $header_value) {
            if ($header_name == 'X_AUTHENTICATION_TOKEN') {
                $request_header = $header_value;
            }
        }
    }
}

So here $request_header will be the token and you can use this in the server side script for validation. 因此,这里$request_header将是令牌,您可以在服务器端脚本中使用它进行验证。

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

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