blob: e117d37c0d67d7c05be74212fd1db02eadf585c2 [file] [log] [blame]
Derek Jones8ede1a22011-10-05 13:34:52 -05001##########
2URL Helper
3##########
4
5The URL Helper file contains functions that assist in working with URLs.
6
Derek Jonesabbf5182013-07-19 16:01:52 -07007.. contents::
8 :local:
9
10.. raw:: html
11
12 <div class="custom-index container"></div>
Derek Jones8ede1a22011-10-05 13:34:52 -050013
14Loading this Helper
15===================
16
Andrey Andreev08f0f8b2012-11-09 10:27:43 +020017This helper is loaded using the following code::
Derek Jones8ede1a22011-10-05 13:34:52 -050018
19 $this->load->helper('url');
20
Derek Jonesabbf5182013-07-19 16:01:52 -070021Available Functions
22===================
Derek Jones8ede1a22011-10-05 13:34:52 -050023
Andrey Andreeve0c566e2016-05-11 16:05:23 +030024The following functions are available:
25
Andrey Andreevcd3d9db2015-02-02 13:41:01 +020026.. php:function:: site_url([$uri = ''[, $protocol = NULL]])
Andrey Andreev08f0f8b2012-11-09 10:27:43 +020027
28 :param string $uri: URI string
Andrey Andreev2023c3d2013-07-18 03:19:59 +030029 :param string $protocol: Protocol, e.g. 'http' or 'https'
Andrey Andreev3de130c2014-02-07 23:31:49 +020030 :returns: Site URL
31 :rtype: string
Andrey Andreev08f0f8b2012-11-09 10:27:43 +020032
Derek Jonesabbf5182013-07-19 16:01:52 -070033 Returns your site URL, as specified in your config file. The index.php
34 file (or whatever you have set as your site **index_page** in your config
35 file) will be added to the URL, as will any URI segments you pass to the
36 function, plus the **url_suffix** as set in your config file.
Derek Jones8ede1a22011-10-05 13:34:52 -050037
Derek Jonesabbf5182013-07-19 16:01:52 -070038 You are encouraged to use this function any time you need to generate a
39 local URL so that your pages become more portable in the event your URL
40 changes.
Derek Jones8ede1a22011-10-05 13:34:52 -050041
Derek Jonesabbf5182013-07-19 16:01:52 -070042 Segments can be optionally passed to the function as a string or an
43 array. Here is a string example::
Derek Jones8ede1a22011-10-05 13:34:52 -050044
Derek Jonesabbf5182013-07-19 16:01:52 -070045 echo site_url('news/local/123');
Derek Jones8ede1a22011-10-05 13:34:52 -050046
Derek Jonesabbf5182013-07-19 16:01:52 -070047 The above example would return something like:
48 *http://example.com/index.php/news/local/123*
Derek Jones8ede1a22011-10-05 13:34:52 -050049
Derek Jonesabbf5182013-07-19 16:01:52 -070050 Here is an example of segments passed as an array::
Derek Jones8ede1a22011-10-05 13:34:52 -050051
Derek Jonesabbf5182013-07-19 16:01:52 -070052 $segments = array('news', 'local', '123');
53 echo site_url($segments);
Derek Jones8ede1a22011-10-05 13:34:52 -050054
Derek Jonesabbf5182013-07-19 16:01:52 -070055 This function is an alias for ``CI_Config::site_url()``. For more info,
56 please see the :doc:`Config Library <../libraries/config>` documentation.
Andrey Andreev08f0f8b2012-11-09 10:27:43 +020057
Andrey Andreevcd3d9db2015-02-02 13:41:01 +020058.. php:function:: base_url($uri = '', $protocol = NULL)
Derek Jones8ede1a22011-10-05 13:34:52 -050059
Andrey Andreev08f0f8b2012-11-09 10:27:43 +020060 :param string $uri: URI string
Andrey Andreev2023c3d2013-07-18 03:19:59 +030061 :param string $protocol: Protocol, e.g. 'http' or 'https'
Andrey Andreev3de130c2014-02-07 23:31:49 +020062 :returns: Base URL
63 :rtype: string
Andrey Andreev08f0f8b2012-11-09 10:27:43 +020064
Derek Jonesabbf5182013-07-19 16:01:52 -070065 Returns your site base URL, as specified in your config file. Example::
Derek Jones8ede1a22011-10-05 13:34:52 -050066
Derek Jonesabbf5182013-07-19 16:01:52 -070067 echo base_url();
Derek Jones8ede1a22011-10-05 13:34:52 -050068
Andrey Andreevcd3d9db2015-02-02 13:41:01 +020069 This function returns the same thing as :php:func:`site_url()`, without
Derek Jonesabbf5182013-07-19 16:01:52 -070070 the *index_page* or *url_suffix* being appended.
Derek Jones8ede1a22011-10-05 13:34:52 -050071
Andrey Andreevcd3d9db2015-02-02 13:41:01 +020072 Also like :php:func:`site_url()`, you can supply segments as a string or
Derek Jonesabbf5182013-07-19 16:01:52 -070073 an array. Here is a string example::
Derek Jones8ede1a22011-10-05 13:34:52 -050074
Derek Jonesabbf5182013-07-19 16:01:52 -070075 echo base_url("blog/post/123");
Derek Jones8ede1a22011-10-05 13:34:52 -050076
Derek Jonesabbf5182013-07-19 16:01:52 -070077 The above example would return something like:
78 *http://example.com/blog/post/123*
Derek Jones8ede1a22011-10-05 13:34:52 -050079
Andrey Andreevcd3d9db2015-02-02 13:41:01 +020080 This is useful because unlike :php:func:`site_url()`, you can supply a
Derek Jonesabbf5182013-07-19 16:01:52 -070081 string to a file, such as an image or stylesheet. For example::
Derek Jones8ede1a22011-10-05 13:34:52 -050082
Derek Jonesabbf5182013-07-19 16:01:52 -070083 echo base_url("images/icons/edit.png");
Derek Jones8ede1a22011-10-05 13:34:52 -050084
Derek Jonesabbf5182013-07-19 16:01:52 -070085 This would give you something like:
86 *http://example.com/images/icons/edit.png*
Andrey Andreev08f0f8b2012-11-09 10:27:43 +020087
Derek Jonesabbf5182013-07-19 16:01:52 -070088 This function is an alias for ``CI_Config::base_url()``. For more info,
89 please see the :doc:`Config Library <../libraries/config>` documentation.
Derek Jones8ede1a22011-10-05 13:34:52 -050090
Andrey Andreevcd3d9db2015-02-02 13:41:01 +020091.. php:function:: current_url()
Andrey Andreev08f0f8b2012-11-09 10:27:43 +020092
Andrey Andreev3de130c2014-02-07 23:31:49 +020093 :returns: The current URL
94 :rtype: string
Andrey Andreev08f0f8b2012-11-09 10:27:43 +020095
Derek Jonesabbf5182013-07-19 16:01:52 -070096 Returns the full URL (including segments) of the page being currently
97 viewed.
Derek Jones8ede1a22011-10-05 13:34:52 -050098
Derek Jonesabbf5182013-07-19 16:01:52 -070099 .. note:: Calling this function is the same as doing this:
100 |
101 | site_url(uri_string());
Andrey Andreev08f0f8b2012-11-09 10:27:43 +0200102
Derek Jones8ede1a22011-10-05 13:34:52 -0500103
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200104.. php:function:: uri_string()
Derek Jones8ede1a22011-10-05 13:34:52 -0500105
Andrey Andreev3de130c2014-02-07 23:31:49 +0200106 :returns: An URI string
107 :rtype: string
Andrey Andreev08f0f8b2012-11-09 10:27:43 +0200108
Derek Jonesabbf5182013-07-19 16:01:52 -0700109 Returns the URI segments of any page that contains this function.
110 For example, if your URL was this::
Derek Jones8ede1a22011-10-05 13:34:52 -0500111
Derek Jonesabbf5182013-07-19 16:01:52 -0700112 http://some-site.com/blog/comments/123
Derek Jones8ede1a22011-10-05 13:34:52 -0500113
Derek Jonesabbf5182013-07-19 16:01:52 -0700114 The function would return::
Derek Jones8ede1a22011-10-05 13:34:52 -0500115
Derek Jonesabbf5182013-07-19 16:01:52 -0700116 blog/comments/123
Derek Jones8ede1a22011-10-05 13:34:52 -0500117
Derek Jonesabbf5182013-07-19 16:01:52 -0700118 This function is an alias for ``CI_Config::uri_string()``. For more info,
119 please see the :doc:`Config Library <../libraries/config>` documentation.
Andrey Andreev08f0f8b2012-11-09 10:27:43 +0200120
Derek Jones8ede1a22011-10-05 13:34:52 -0500121
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200122.. php:function:: index_page()
Derek Jones8ede1a22011-10-05 13:34:52 -0500123
Andrey Andreev3de130c2014-02-07 23:31:49 +0200124 :returns: 'index_page' value
125 :rtype: mixed
Andrey Andreev08f0f8b2012-11-09 10:27:43 +0200126
Derek Jonesabbf5182013-07-19 16:01:52 -0700127 Returns your site **index_page**, as specified in your config file.
128 Example::
Derek Jones8ede1a22011-10-05 13:34:52 -0500129
Derek Jonesabbf5182013-07-19 16:01:52 -0700130 echo index_page();
Derek Jones8ede1a22011-10-05 13:34:52 -0500131
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200132.. php:function:: anchor($uri = '', $title = '', $attributes = '')
Derek Jones8ede1a22011-10-05 13:34:52 -0500133
Andrey Andreev08f0f8b2012-11-09 10:27:43 +0200134 :param string $uri: URI string
135 :param string $title: Anchor title
136 :param mixed $attributes: HTML attributes
Andrey Andreev3de130c2014-02-07 23:31:49 +0200137 :returns: HTML hyperlink (anchor tag)
138 :rtype: string
Derek Jones8ede1a22011-10-05 13:34:52 -0500139
Derek Jonesabbf5182013-07-19 16:01:52 -0700140 Creates a standard HTML anchor link based on your local site URL.
Derek Jones8ede1a22011-10-05 13:34:52 -0500141
Derek Jonesabbf5182013-07-19 16:01:52 -0700142 The first parameter can contain any segments you wish appended to the
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200143 URL. As with the :php:func:`site_url()` function above, segments can
Derek Jonesabbf5182013-07-19 16:01:52 -0700144 be a string or an array.
Derek Jones8ede1a22011-10-05 13:34:52 -0500145
Derek Jonesabbf5182013-07-19 16:01:52 -0700146 .. note:: If you are building links that are internal to your application
Andrey Andreev84760562018-02-12 15:15:47 +0200147 do not include the base URL (\http://...). This will be added
Derek Jonesabbf5182013-07-19 16:01:52 -0700148 automatically from the information specified in your config file.
149 Include only the URI segments you wish appended to the URL.
Derek Jones8ede1a22011-10-05 13:34:52 -0500150
Derek Jonesabbf5182013-07-19 16:01:52 -0700151 The second segment is the text you would like the link to say. If you
152 leave it blank, the URL will be used.
Derek Jones8ede1a22011-10-05 13:34:52 -0500153
Derek Jonesabbf5182013-07-19 16:01:52 -0700154 The third parameter can contain a list of attributes you would like
155 added to the link. The attributes can be a simple string or an
156 associative array.
Derek Jones8ede1a22011-10-05 13:34:52 -0500157
Derek Jonesabbf5182013-07-19 16:01:52 -0700158 Here are some examples::
Derek Jones8ede1a22011-10-05 13:34:52 -0500159
Derek Jonesabbf5182013-07-19 16:01:52 -0700160 echo anchor('news/local/123', 'My News', 'title="News title"');
161 // Prints: <a href="http://example.com/index.php/news/local/123" title="News title">My News</a>
Derek Jones8ede1a22011-10-05 13:34:52 -0500162
Derek Jonesabbf5182013-07-19 16:01:52 -0700163 echo anchor('news/local/123', 'My News', array('title' => 'The best news!'));
164 // Prints: <a href="http://example.com/index.php/news/local/123" title="The best news!">My News</a>
Derek Jones8ede1a22011-10-05 13:34:52 -0500165
Derek Jonesabbf5182013-07-19 16:01:52 -0700166 echo anchor('', 'Click here');
167 // Prints: <a href="http://example.com">Click Here</a>
Derek Jones8ede1a22011-10-05 13:34:52 -0500168
Derek Jones8ede1a22011-10-05 13:34:52 -0500169
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200170.. php:function:: anchor_popup($uri = '', $title = '', $attributes = FALSE)
Derek Jones8ede1a22011-10-05 13:34:52 -0500171
Andrey Andreev08f0f8b2012-11-09 10:27:43 +0200172 :param string $uri: URI string
173 :param string $title: Anchor title
174 :param mixed $attributes: HTML attributes
Andrey Andreev3de130c2014-02-07 23:31:49 +0200175 :returns: Pop-up hyperlink
176 :rtype: string
Andrey Andreev08f0f8b2012-11-09 10:27:43 +0200177
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200178 Nearly identical to the :php:func:`anchor()` function except that it
Derek Jonesabbf5182013-07-19 16:01:52 -0700179 opens the URL in a new window. You can specify JavaScript window
180 attributes in the third parameter to control how the window is opened.
181 If the third parameter is not set it will simply open a new window with
182 your own browser settings.
Andrey Andreev08f0f8b2012-11-09 10:27:43 +0200183
Derek Jonesabbf5182013-07-19 16:01:52 -0700184 Here is an example with attributes::
Derek Jones8ede1a22011-10-05 13:34:52 -0500185
Derek Jonesabbf5182013-07-19 16:01:52 -0700186 $atts = array(
187 'width' => 800,
188 'height' => 600,
189 'scrollbars' => 'yes',
190 'status'      => 'yes',
191 'resizable'   => 'yes',
192 'screenx' => 0,
193 'screeny' => 0,
194 'window_name' => '_blank'
195 );
Derek Jones8ede1a22011-10-05 13:34:52 -0500196
Derek Jonesabbf5182013-07-19 16:01:52 -0700197 echo anchor_popup('news/local/123', 'Click Me!', $atts);
Derek Jones8ede1a22011-10-05 13:34:52 -0500198
Derek Jonesabbf5182013-07-19 16:01:52 -0700199 .. note:: The above attributes are the function defaults so you only need to
200 set the ones that are different from what you need. If you want the
201 function to use all of its defaults simply pass an empty array in the
202 third parameter:
203 |
204 | echo anchor_popup('news/local/123', 'Click Me!', array());
Derek Jones8ede1a22011-10-05 13:34:52 -0500205
Derek Jonesabbf5182013-07-19 16:01:52 -0700206 .. note:: The **window_name** is not really an attribute, but an argument to
207 the JavaScript `window.open() <http://www.w3schools.com/jsref/met_win_open.asp>`
208 method, which accepts either a window name or a window target.
Andrey Andreev81c32082012-06-16 21:21:46 +0300209
Derek Jonesabbf5182013-07-19 16:01:52 -0700210 .. note:: Any other attribute than the listed above will be parsed as an
211 HTML attribute to the anchor tag.
Andrey Andreev81c32082012-06-16 21:21:46 +0300212
Derek Jones8ede1a22011-10-05 13:34:52 -0500213
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200214.. php:function:: mailto($email, $title = '', $attributes = '')
Derek Jones8ede1a22011-10-05 13:34:52 -0500215
Andrey Andreev08f0f8b2012-11-09 10:27:43 +0200216 :param string $email: E-mail address
217 :param string $title: Anchor title
218 :param mixed $attributes: HTML attributes
Andrey Andreev3de130c2014-02-07 23:31:49 +0200219 :returns: A "mail to" hyperlink
220 :rtype: string
Andrey Andreev08f0f8b2012-11-09 10:27:43 +0200221
Derek Jonesabbf5182013-07-19 16:01:52 -0700222 Creates a standard HTML e-mail link. Usage example::
Derek Jones8ede1a22011-10-05 13:34:52 -0500223
Derek Jonesabbf5182013-07-19 16:01:52 -0700224 echo mailto('me@my-site.com', 'Click Here to Contact Me');
Derek Jones8ede1a22011-10-05 13:34:52 -0500225
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200226 As with the :php:func:`anchor()` tab above, you can set attributes using the
Derek Jonesabbf5182013-07-19 16:01:52 -0700227 third parameter::
InFog00b3df42012-07-29 13:42:50 -0300228
Derek Jonesabbf5182013-07-19 16:01:52 -0700229 $attributes = array('title' => 'Mail me');
230 echo mailto('me@my-site.com', 'Contact Me', $attributes);
Derek Jones8ede1a22011-10-05 13:34:52 -0500231
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200232.. php:function:: safe_mailto($email, $title = '', $attributes = '')
Andrey Andreev08f0f8b2012-11-09 10:27:43 +0200233
234 :param string $email: E-mail address
235 :param string $title: Anchor title
236 :param mixed $attributes: HTML attributes
Andrey Andreev3de130c2014-02-07 23:31:49 +0200237 :returns: A spam-safe "mail to" hyperlink
238 :rtype: string
Andrey Andreev08f0f8b2012-11-09 10:27:43 +0200239
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200240 Identical to the :php:func:`mailto()` function except it writes an obfuscated
Derek Jonesabbf5182013-07-19 16:01:52 -0700241 version of the *mailto* tag using ordinal numbers written with JavaScript to
242 help prevent the e-mail address from being harvested by spam bots.
Derek Jones8ede1a22011-10-05 13:34:52 -0500243
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200244.. php:function:: auto_link($str, $type = 'both', $popup = FALSE)
Derek Jones8ede1a22011-10-05 13:34:52 -0500245
Andrey Andreev08f0f8b2012-11-09 10:27:43 +0200246 :param string $str: Input string
247 :param string $type: Link type ('email', 'url' or 'both')
248 :param bool $popup: Whether to create popup links
Andrey Andreev3de130c2014-02-07 23:31:49 +0200249 :returns: Linkified string
250 :rtype: string
Andrey Andreev08f0f8b2012-11-09 10:27:43 +0200251
Derek Jonesabbf5182013-07-19 16:01:52 -0700252 Automatically turns URLs and e-mail addresses contained in a string into
253 links. Example::
Derek Jones8ede1a22011-10-05 13:34:52 -0500254
Derek Jonesabbf5182013-07-19 16:01:52 -0700255 $string = auto_link($string);
Derek Jones8ede1a22011-10-05 13:34:52 -0500256
Derek Jonesabbf5182013-07-19 16:01:52 -0700257 The second parameter determines whether URLs and e-mails are converted or
258 just one or the other. Default behavior is both if the parameter is not
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200259 specified. E-mail links are encoded as :php:func:`safe_mailto()` as shown
Derek Jonesabbf5182013-07-19 16:01:52 -0700260 above.
Derek Jones8ede1a22011-10-05 13:34:52 -0500261
Derek Jonesabbf5182013-07-19 16:01:52 -0700262 Converts only URLs::
Derek Jones8ede1a22011-10-05 13:34:52 -0500263
Derek Jonesabbf5182013-07-19 16:01:52 -0700264 $string = auto_link($string, 'url');
Derek Jones8ede1a22011-10-05 13:34:52 -0500265
Derek Jonesabbf5182013-07-19 16:01:52 -0700266 Converts only e-mail addresses::
Derek Jones8ede1a22011-10-05 13:34:52 -0500267
Derek Jonesabbf5182013-07-19 16:01:52 -0700268 $string = auto_link($string, 'email');
Derek Jones8ede1a22011-10-05 13:34:52 -0500269
Derek Jonesabbf5182013-07-19 16:01:52 -0700270 The third parameter determines whether links are shown in a new window.
271 The value can be TRUE or FALSE (boolean)::
Derek Jones8ede1a22011-10-05 13:34:52 -0500272
Derek Jonesabbf5182013-07-19 16:01:52 -0700273 $string = auto_link($string, 'both', TRUE);
Derek Jones8ede1a22011-10-05 13:34:52 -0500274
Derek Jones8ede1a22011-10-05 13:34:52 -0500275
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200276.. php:function:: url_title($str, $separator = '-', $lowercase = FALSE)
Andrey Andreev08f0f8b2012-11-09 10:27:43 +0200277
278 :param string $str: Input string
279 :param string $separator: Word separator
Andrey Andreev93438512016-04-28 11:09:06 +0300280 :param bool $lowercase: Whether to transform the output string to lower-case
Andrey Andreev3de130c2014-02-07 23:31:49 +0200281 :returns: URL-formatted string
282 :rtype: string
Andrey Andreev08f0f8b2012-11-09 10:27:43 +0200283
Derek Jonesabbf5182013-07-19 16:01:52 -0700284 Takes a string as input and creates a human-friendly URL string. This is
285 useful if, for example, you have a blog in which you'd like to use the
286 title of your entries in the URL. Example::
Derek Jones8ede1a22011-10-05 13:34:52 -0500287
Derek Jonesabbf5182013-07-19 16:01:52 -0700288 $title = "What's wrong with CSS?";
289 $url_title = url_title($title);
290 // Produces: Whats-wrong-with-CSS
Derek Jones8ede1a22011-10-05 13:34:52 -0500291
Derek Jonesabbf5182013-07-19 16:01:52 -0700292 The second parameter determines the word delimiter. By default dashes
293 are used. Preferred options are: **-** (dash) or **_** (underscore)
Derek Jones8ede1a22011-10-05 13:34:52 -0500294
Derek Jonesabbf5182013-07-19 16:01:52 -0700295 Example::
Derek Jones8ede1a22011-10-05 13:34:52 -0500296
Derek Jonesabbf5182013-07-19 16:01:52 -0700297 $title = "What's wrong with CSS?";
298 $url_title = url_title($title, 'underscore');
299 // Produces: Whats_wrong_with_CSS
Andrey Andreev08f0f8b2012-11-09 10:27:43 +0200300
Derek Jonesabbf5182013-07-19 16:01:52 -0700301 .. note:: Old usage of 'dash' and 'underscore' as the second parameter
302 is DEPRECATED.
Derek Jones8ede1a22011-10-05 13:34:52 -0500303
Derek Jonesabbf5182013-07-19 16:01:52 -0700304 The third parameter determines whether or not lowercase characters are
305 forced. By default they are not. Options are boolean TRUE/FALSE.
Derek Jones8ede1a22011-10-05 13:34:52 -0500306
Derek Jonesabbf5182013-07-19 16:01:52 -0700307 Example::
Derek Jones8ede1a22011-10-05 13:34:52 -0500308
Derek Jonesabbf5182013-07-19 16:01:52 -0700309 $title = "What's wrong with CSS?";
310 $url_title = url_title($title, 'underscore', TRUE);
311 // Produces: whats_wrong_with_css
Derek Jones8ede1a22011-10-05 13:34:52 -0500312
Derek Jones8ede1a22011-10-05 13:34:52 -0500313
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200314.. php:function:: prep_url($str = '')
Derek Jones8ede1a22011-10-05 13:34:52 -0500315
Andrey Andreev08f0f8b2012-11-09 10:27:43 +0200316 :param string $str: URL string
Andrey Andreev3de130c2014-02-07 23:31:49 +0200317 :returns: Protocol-prefixed URL string
318 :rtype: string
Derek Jones8ede1a22011-10-05 13:34:52 -0500319
Andrey Andreev84760562018-02-12 15:15:47 +0200320 This function will add \http:// in the event that a protocol prefix
Derek Jonesabbf5182013-07-19 16:01:52 -0700321 is missing from a URL.
Andrey Andreev08f0f8b2012-11-09 10:27:43 +0200322
Derek Jonesabbf5182013-07-19 16:01:52 -0700323 Pass the URL string to the function like this::
Andrey Andreev08f0f8b2012-11-09 10:27:43 +0200324
Derek Jonesabbf5182013-07-19 16:01:52 -0700325 $url = prep_url('example.com');
Derek Jones8ede1a22011-10-05 13:34:52 -0500326
Derek Jones8ede1a22011-10-05 13:34:52 -0500327
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200328.. php:function:: redirect($uri = '', $method = 'auto', $code = NULL)
Andrey Andreev08f0f8b2012-11-09 10:27:43 +0200329
330 :param string $uri: URI string
331 :param string $method: Redirect method ('auto', 'location' or 'refresh')
332 :param string $code: HTTP Response code (usually 302 or 303)
Andrey Andreev3de130c2014-02-07 23:31:49 +0200333 :rtype: void
Andrey Andreev08f0f8b2012-11-09 10:27:43 +0200334
Derek Jonesabbf5182013-07-19 16:01:52 -0700335 Does a "header redirect" to the URI specified. If you specify the full
336 site URL that link will be built, but for local links simply providing
337 the URI segments to the controller you want to direct to will create the
338 link. The function will build the URL based on your config file values.
Derek Jones8ede1a22011-10-05 13:34:52 -0500339
Derek Jonesabbf5182013-07-19 16:01:52 -0700340 The optional second parameter allows you to force a particular redirection
341 method. The available methods are **auto**, **location** and **refresh**,
342 with location being faster but less reliable on IIS servers.
343 The default is **auto**, which will attempt to intelligently choose the
344 method based on the server environment.
Derek Jones8ede1a22011-10-05 13:34:52 -0500345
Derek Jonesabbf5182013-07-19 16:01:52 -0700346 The optional third parameter allows you to send a specific HTTP Response
347 Code - this could be used for example to create 301 redirects for search
348 engine purposes. The default Response Code is 302. The third parameter is
349 *only* available with **location** redirects, and not *refresh*. Examples::
Derek Jones8ede1a22011-10-05 13:34:52 -0500350
Derek Jonesabbf5182013-07-19 16:01:52 -0700351 if ($logged_in == FALSE)
352 {      
353 redirect('/login/form/');
354 }
Derek Jones8ede1a22011-10-05 13:34:52 -0500355
Derek Jonesabbf5182013-07-19 16:01:52 -0700356 // with 301 redirect
357 redirect('/article/13', 'location', 301);
Derek Jones8ede1a22011-10-05 13:34:52 -0500358
Derek Jonesabbf5182013-07-19 16:01:52 -0700359 .. note:: In order for this function to work it must be used before anything
360 is outputted to the browser since it utilizes server headers.
Derek Jones8ede1a22011-10-05 13:34:52 -0500361
Derek Jonesabbf5182013-07-19 16:01:52 -0700362 .. note:: For very fine grained control over headers, you should use the
Andrey Andreeve0c566e2016-05-11 16:05:23 +0300363 :doc:`Output Library </libraries/output>` ``set_header()`` method.
vlakoff530b9462012-09-17 14:18:07 +0200364
Derek Jonesabbf5182013-07-19 16:01:52 -0700365 .. note:: To IIS users: if you hide the `Server` HTTP header, the *auto*
366 method won't detect IIS, in that case it is advised you explicitly
367 use the **refresh** method.
Andrey Andreev08f0f8b2012-11-09 10:27:43 +0200368
Derek Jonesabbf5182013-07-19 16:01:52 -0700369 .. note:: When the **location** method is used, an HTTP status code of 303
370 will *automatically* be selected when the page is currently accessed
371 via POST and HTTP/1.1 is used.
Andrey Andreev08f0f8b2012-11-09 10:27:43 +0200372
Andrey Andreev93438512016-04-28 11:09:06 +0300373 .. important:: This function will terminate script execution.