简体   繁体   English

使用CodeIgniter获取URL段并将其存储到Cookie中

[英]Get and store a URL segment into a cookie with CodeIgniter

For a kind of conversion tracking, I want to store a part of the URL into a cookie and use this into my model functions. 对于一种转换跟踪,我想将一部分URL存储到cookie中,并将其用于我的模型函数中。

So I think about https//:www.example.com?track=this 所以我想到了https//:www.example.com?track=this

How I can get the part of the track (=this) with CodeIgniter and save it into a cookie? 如何使用CodeIgniter获取曲目的一部分(=this)并将其保存到cookie中?

To create a cookie I think that's the way, 我认为就是要创建一个cookie,

$this->load->helper('cookie');
                $cookie = array(
                        'name'   => 'track',
                        'value'  => '???',                            
                        'expire' => '300',                                                                                   
                        'secure' => TRUE
                        );
               set_cookie($cookie);  

How I can get the part of URL and set cookie etc with CodeIgniter on every possible page? 如何在每个可能的页面上使用CodeIgniter获取URL的一部分并设置Cookie等? So I mean, no matter over which page the user call the website, I need to make sure that this tracking info is stored in every case. 因此,我的意思是,无论用户在网站上访问哪个页面,我都需要确保在每种情况下都存储此跟踪信息。

Go get the cookie later, I think I can use $cookie= get_cookie('track'); 以后去获取cookie,我想我可以使用$cookie= get_cookie('track');

Thanks for showing me the way to do it with CodeIgniter. 感谢您向我展示使用CodeIgniter的方法。

Mainly you need to use parse_url to extract url to query string and then parse_str to extract query string . 主要是需要使用parse_url提取urlquery string ,然后使用parse_str提取query string

First, you need to get query string from you URL 首先,您需要从URL获取query string

$query = parse_url('https//:www.example.com?track=this&h=1');
echo'<pre>';print_r($query);

Output: 输出:

Array
(
    [path] => https//:www.example.com
    [query] => track=this&h=1
)

Then you need to extract query 然后您需要提取query

parse_str($query['query'],$array);
echo'<pre>';print_r($array);die;

Output: 输出:

Array
(
    [track] => this
    [h] => 1
)

From here you can store your above array information in cookies 在这里,您可以将上述数组信息存储在cookies

to get the wanted part of the query-string there is a built-in function of Codeigniter 要获得查询字符串的所需部分,有一个Codeigniter内置函数

$myval=$this->input->get('track', TRUE);

I've add this with a part to store and get the cookie in the header of my mainlayout-theme. 我将其添加一部分以存储并在我的mainlayout-theme的标题中获取cookie。

So it will works :-) 这样就可以了:-)

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

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