blob: 7a49f188da45c3317a79904c14f868bab081f213 [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
7.. contents:: Page Contents
8
9Loading this Helper
10===================
11
Andrey Andreev08f0f8b2012-11-09 10:27:43 +020012This helper is loaded using the following code::
Derek Jones8ede1a22011-10-05 13:34:52 -050013
14 $this->load->helper('url');
15
16The following functions are available:
17
18site_url()
19==========
20
Andrey Andreev2023c3d2013-07-18 03:19:59 +030021.. php:function:: site_url($uri = '', $protocol = NULL)
Andrey Andreev08f0f8b2012-11-09 10:27:43 +020022
23 :param string $uri: URI string
Andrey Andreev2023c3d2013-07-18 03:19:59 +030024 :param string $protocol: Protocol, e.g. 'http' or 'https'
Andrey Andreev08f0f8b2012-11-09 10:27:43 +020025 :returns: string
26
Derek Jones8ede1a22011-10-05 13:34:52 -050027Returns your site URL, as specified in your config file. The index.php
Andrey Andreev08f0f8b2012-11-09 10:27:43 +020028file (or whatever you have set as your site **index_page** in your config
Derek Jones8ede1a22011-10-05 13:34:52 -050029file) will be added to the URL, as will any URI segments you pass to the
Andrey Andreev08f0f8b2012-11-09 10:27:43 +020030function, plus the **url_suffix** as set in your config file.
Derek Jones8ede1a22011-10-05 13:34:52 -050031
32You are encouraged to use this function any time you need to generate a
33local URL so that your pages become more portable in the event your URL
34changes.
35
36Segments can be optionally passed to the function as a string or an
Andrey Andreev08f0f8b2012-11-09 10:27:43 +020037array. Here is a string example::
Derek Jones8ede1a22011-10-05 13:34:52 -050038
Andrey Andreev08f0f8b2012-11-09 10:27:43 +020039 echo site_url('news/local/123');
Derek Jones8ede1a22011-10-05 13:34:52 -050040
41The above example would return something like:
Andrey Andreev08f0f8b2012-11-09 10:27:43 +020042*http://example.com/index.php/news/local/123*
Derek Jones8ede1a22011-10-05 13:34:52 -050043
Andrey Andreev08f0f8b2012-11-09 10:27:43 +020044Here is an example of segments passed as an array::
Derek Jones8ede1a22011-10-05 13:34:52 -050045
46 $segments = array('news', 'local', '123');
47 echo site_url($segments);
48
Andrey Andreev08f0f8b2012-11-09 10:27:43 +020049This function is an alias for ``CI_Config::site_url()``. For more info,
50please see the :doc:`Config Library <../libraries/config>` documentation.
51
Derek Jones8ede1a22011-10-05 13:34:52 -050052base_url()
53===========
54
Andrey Andreev2023c3d2013-07-18 03:19:59 +030055.. php:function:: base_url($uri = '', $protocol = NULL)
Derek Jones8ede1a22011-10-05 13:34:52 -050056
Andrey Andreev08f0f8b2012-11-09 10:27:43 +020057 :param string $uri: URI string
Andrey Andreev2023c3d2013-07-18 03:19:59 +030058 :param string $protocol: Protocol, e.g. 'http' or 'https'
Andrey Andreev08f0f8b2012-11-09 10:27:43 +020059 :returns: string
60
61Returns your site base URL, as specified in your config file. Example::
Derek Jones8ede1a22011-10-05 13:34:52 -050062
63 echo base_url();
64
Andrey Andreev08f0f8b2012-11-09 10:27:43 +020065This function returns the same thing as :php:func:`site_url()`, without
66the *index_page* or *url_suffix* being appended.
Derek Jones8ede1a22011-10-05 13:34:52 -050067
Andrey Andreev08f0f8b2012-11-09 10:27:43 +020068Also like :php:func:`site_url()`, you can supply segments as a string or
69an array. Here is a string example::
Derek Jones8ede1a22011-10-05 13:34:52 -050070
71 echo base_url("blog/post/123");
72
73The above example would return something like:
Andrey Andreev08f0f8b2012-11-09 10:27:43 +020074*http://example.com/blog/post/123*
Derek Jones8ede1a22011-10-05 13:34:52 -050075
Andrey Andreev08f0f8b2012-11-09 10:27:43 +020076This is useful because unlike :php:func:`site_url()`, you can supply a
77string to a file, such as an image or stylesheet. For example::
Derek Jones8ede1a22011-10-05 13:34:52 -050078
79 echo base_url("images/icons/edit.png");
80
81This would give you something like:
Andrey Andreev08f0f8b2012-11-09 10:27:43 +020082*http://example.com/images/icons/edit.png*
83
84This function is an alias for ``CI_Config::base_url()``. For more info,
85please see the :doc:`Config Library <../libraries/config>` documentation.
Derek Jones8ede1a22011-10-05 13:34:52 -050086
87current_url()
88=============
89
Andrey Andreev08f0f8b2012-11-09 10:27:43 +020090.. php:function:: current_url()
91
92 :returns: string
93
Derek Jones8ede1a22011-10-05 13:34:52 -050094Returns the full URL (including segments) of the page being currently
95viewed.
96
Andrey Andreev08f0f8b2012-11-09 10:27:43 +020097.. note:: Calling this function is the same as doing this:
98 |
99 | site_url(uri_string());
100
Derek Jones8ede1a22011-10-05 13:34:52 -0500101uri_string()
102============
103
Andrey Andreev08f0f8b2012-11-09 10:27:43 +0200104.. php:function:: uri_string()
Derek Jones8ede1a22011-10-05 13:34:52 -0500105
Andrey Andreev08f0f8b2012-11-09 10:27:43 +0200106 :returns: string
107
108Returns the URI segments of any page that contains this function.
109For example, if your URL was this::
Derek Jones8ede1a22011-10-05 13:34:52 -0500110
111 http://some-site.com/blog/comments/123
112
Andrey Andreev08f0f8b2012-11-09 10:27:43 +0200113The function would return::
Derek Jones8ede1a22011-10-05 13:34:52 -0500114
Melvin Lammerts3b1e6e62013-01-23 22:46:16 +0100115 blog/comments/123
Derek Jones8ede1a22011-10-05 13:34:52 -0500116
Andrey Andreev08f0f8b2012-11-09 10:27:43 +0200117This function is an alias for ``CI_Config::uri_string()``. For more info,
118please see the :doc:`Config Library <../libraries/config>` documentation.
119
Derek Jones8ede1a22011-10-05 13:34:52 -0500120index_page()
121============
122
Andrey Andreev08f0f8b2012-11-09 10:27:43 +0200123.. php:function:: index_page()
Derek Jones8ede1a22011-10-05 13:34:52 -0500124
Andrey Andreev08f0f8b2012-11-09 10:27:43 +0200125 :returns: string
126
127Returns your site **index_page**, as specified in your config file.
128Example::
Derek Jones8ede1a22011-10-05 13:34:52 -0500129
130 echo index_page();
131
132anchor()
133========
134
Andrey Andreev08f0f8b2012-11-09 10:27:43 +0200135.. php:function:: anchor($uri = '', $title = '', $attributes = '')
Derek Jones8ede1a22011-10-05 13:34:52 -0500136
Andrey Andreev08f0f8b2012-11-09 10:27:43 +0200137 :param string $uri: URI string
138 :param string $title: Anchor title
139 :param mixed $attributes: HTML attributes
140 :returns: string
Derek Jones8ede1a22011-10-05 13:34:52 -0500141
Andrey Andreev08f0f8b2012-11-09 10:27:43 +0200142Creates a standard HTML anchor link based on your local site URL.
Derek Jones8ede1a22011-10-05 13:34:52 -0500143
144The first parameter can contain any segments you wish appended to the
Andrey Andreev08f0f8b2012-11-09 10:27:43 +0200145URL. As with the :php:func:`site_url()` function above, segments can
146be a string or an array.
Derek Jones8ede1a22011-10-05 13:34:52 -0500147
148.. note:: If you are building links that are internal to your application
Andrey Andreev08f0f8b2012-11-09 10:27:43 +0200149 do not include the base URL (http://...). This will be added
150 automatically from the information specified in your config file.
151 Include only the URI segments you wish appended to the URL.
Derek Jones8ede1a22011-10-05 13:34:52 -0500152
153The second segment is the text you would like the link to say. If you
154leave it blank, the URL will be used.
155
156The third parameter can contain a list of attributes you would like
157added to the link. The attributes can be a simple string or an
158associative array.
159
Andrey Andreev08f0f8b2012-11-09 10:27:43 +0200160Here are some examples::
Derek Jones8ede1a22011-10-05 13:34:52 -0500161
162 echo anchor('news/local/123', 'My News', 'title="News title"');
Andrey Andreev08f0f8b2012-11-09 10:27:43 +0200163 // Prints: <a href="http://example.com/index.php/news/local/123" title="News title">My News</a>
Derek Jones8ede1a22011-10-05 13:34:52 -0500164
165 echo anchor('news/local/123', 'My News', array('title' => 'The best news!'));
Andrey Andreev08f0f8b2012-11-09 10:27:43 +0200166 // 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 -0500167
Andrey Andreev08f0f8b2012-11-09 10:27:43 +0200168 echo anchor('', 'Click here');
169 // Prints: <a href="http://example.com">Click Here</a>
Derek Jones8ede1a22011-10-05 13:34:52 -0500170
171anchor_popup()
172==============
173
Andrey Andreev08f0f8b2012-11-09 10:27:43 +0200174.. php:function:: anchor_popup($uri = '', $title = '', $attributes = FALSE)
Derek Jones8ede1a22011-10-05 13:34:52 -0500175
Andrey Andreev08f0f8b2012-11-09 10:27:43 +0200176 :param string $uri: URI string
177 :param string $title: Anchor title
178 :param mixed $attributes: HTML attributes
179 :returns: string
180
181Nearly identical to the :php:func:``anchor()`` function except that it
182opens the URL in a new window. You can specify JavaScript window
183attributes in the third parameter to control how the window is opened.
184If the third parameter is not set it will simply open a new window with
185your own browser settings.
186
187Here is an example with attributes::
Derek Jones8ede1a22011-10-05 13:34:52 -0500188
Andrey Andreev81c32082012-06-16 21:21:46 +0300189 $atts = array(
Andrey Andreev08f0f8b2012-11-09 10:27:43 +0200190 'width' => 800,
191 'height' => 600,
Andrey Andreev81c32082012-06-16 21:21:46 +0300192 'scrollbars' => 'yes',
193 'status'      => 'yes',
194 'resizable'   => 'yes',
Andrey Andreev08f0f8b2012-11-09 10:27:43 +0200195 'screenx' => 0,
196 'screeny' => 0,
Andrey Andreev81c32082012-06-16 21:21:46 +0300197 'window_name' => '_blank'
Derek Jones8ede1a22011-10-05 13:34:52 -0500198 );
199
200 echo anchor_popup('news/local/123', 'Click Me!', $atts);
201
Andrey Andreev81c32082012-06-16 21:21:46 +0300202.. note:: The above attributes are the function defaults so you only need to
Derek Jonesce79be02012-06-25 23:23:46 -0700203 set the ones that are different from what you need. If you want the
204 function to use all of its defaults simply pass an empty array in the
Andrey Andreev08f0f8b2012-11-09 10:27:43 +0200205 third parameter:
206 |
207 | echo anchor_popup('news/local/123', 'Click Me!', array());
Derek Jones8ede1a22011-10-05 13:34:52 -0500208
Andrey Andreev08f0f8b2012-11-09 10:27:43 +0200209.. note:: The **window_name** is not really an attribute, but an argument to
Andrey Andreev81c32082012-06-16 21:21:46 +0300210 the JavaScript `window.open() <http://www.w3schools.com/jsref/met_win_open.asp>`
211 method, which accepts either a window name or a window target.
212
213.. note:: Any other attribute than the listed above will be parsed as an
214 HTML attribute to the anchor tag.
215
Derek Jones8ede1a22011-10-05 13:34:52 -0500216mailto()
217========
218
Andrey Andreev08f0f8b2012-11-09 10:27:43 +0200219.. php:function:: mailto($email, $title = '', $attributes = '')
Derek Jones8ede1a22011-10-05 13:34:52 -0500220
Andrey Andreev08f0f8b2012-11-09 10:27:43 +0200221 :param string $email: E-mail address
222 :param string $title: Anchor title
223 :param mixed $attributes: HTML attributes
224 :returns: string
225
226Creates a standard HTML e-mail link. Usage example::
Derek Jones8ede1a22011-10-05 13:34:52 -0500227
228 echo mailto('me@my-site.com', 'Click Here to Contact Me');
229
Andrey Andreev08f0f8b2012-11-09 10:27:43 +0200230As with the :php:func:`anchor()` tab above, you can set attributes using the
231third parameter::
InFog00b3df42012-07-29 13:42:50 -0300232
Andrey Andreev08f0f8b2012-11-09 10:27:43 +0200233 $attributes = array('title' => 'Mail me');
234 echo mailto('me@my-site.com', 'Contact Me', $attributes);
Derek Jones8ede1a22011-10-05 13:34:52 -0500235
236safe_mailto()
237=============
238
Andrey Andreev08f0f8b2012-11-09 10:27:43 +0200239.. php:function:: safe_mailto($email, $title = '', $attributes = '')
240
241 :param string $email: E-mail address
242 :param string $title: Anchor title
243 :param mixed $attributes: HTML attributes
244 :returns: string
245
246Identical to the :php:func:`mailto()` function except it writes an obfuscated
247version of the *mailto* tag using ordinal numbers written with JavaScript to
248help prevent the e-mail address from being harvested by spam bots.
Derek Jones8ede1a22011-10-05 13:34:52 -0500249
250auto_link()
251===========
252
Andrey Andreev08f0f8b2012-11-09 10:27:43 +0200253.. php:function:: auto_link($str, $type = 'both', $popup = FALSE)
Derek Jones8ede1a22011-10-05 13:34:52 -0500254
Andrey Andreev08f0f8b2012-11-09 10:27:43 +0200255 :param string $str: Input string
256 :param string $type: Link type ('email', 'url' or 'both')
257 :param bool $popup: Whether to create popup links
258 :returns: string
259
260Automatically turns URLs and e-mail addresses contained in a string into
261links. Example::
Derek Jones8ede1a22011-10-05 13:34:52 -0500262
263 $string = auto_link($string);
264
Andrey Andreev08f0f8b2012-11-09 10:27:43 +0200265The second parameter determines whether URLs and e-mails are converted or
Derek Jones8ede1a22011-10-05 13:34:52 -0500266just one or the other. Default behavior is both if the parameter is not
Andrey Andreev08f0f8b2012-11-09 10:27:43 +0200267specified. E-mail links are encoded as :php:func:`safe_mailto()` as shown
268above.
Derek Jones8ede1a22011-10-05 13:34:52 -0500269
Andrey Andreev08f0f8b2012-11-09 10:27:43 +0200270Converts only URLs::
Derek Jones8ede1a22011-10-05 13:34:52 -0500271
272 $string = auto_link($string, 'url');
273
Andrey Andreev08f0f8b2012-11-09 10:27:43 +0200274Converts only e-mail addresses::
Derek Jones8ede1a22011-10-05 13:34:52 -0500275
276 $string = auto_link($string, 'email');
277
278The third parameter determines whether links are shown in a new window.
Andrey Andreev08f0f8b2012-11-09 10:27:43 +0200279The value can be TRUE or FALSE (boolean)::
Derek Jones8ede1a22011-10-05 13:34:52 -0500280
281 $string = auto_link($string, 'both', TRUE);
282
283url_title()
284===========
285
Andrey Andreev08f0f8b2012-11-09 10:27:43 +0200286.. php:function:: url_title($str, $separator = '-', $lowercase = FALSE)
287
288 :param string $str: Input string
289 :param string $separator: Word separator
290 :param string $lowercase: Whether to transform the output string to lower-case
291 :returns: string
292
Derek Jones8ede1a22011-10-05 13:34:52 -0500293Takes a string as input and creates a human-friendly URL string. This is
294useful if, for example, you have a blog in which you'd like to use the
Andrey Andreev08f0f8b2012-11-09 10:27:43 +0200295title of your entries in the URL. Example::
Derek Jones8ede1a22011-10-05 13:34:52 -0500296
297 $title = "What's wrong with CSS?";
Andrey Andreev08f0f8b2012-11-09 10:27:43 +0200298 $url_title = url_title($title);
299 // Produces: Whats-wrong-with-CSS
Derek Jones8ede1a22011-10-05 13:34:52 -0500300
301The second parameter determines the word delimiter. By default dashes
Andrey Andreev08f0f8b2012-11-09 10:27:43 +0200302are used. Preferred options are: **-** (dash) or **_** (underscore)
Derek Jones8ede1a22011-10-05 13:34:52 -0500303
Andrey Andreev08f0f8b2012-11-09 10:27:43 +0200304Example::
Derek Jones8ede1a22011-10-05 13:34:52 -0500305
306 $title = "What's wrong with CSS?";
Andrey Andreev08f0f8b2012-11-09 10:27:43 +0200307 $url_title = url_title($title, 'underscore');
308 // Produces: Whats_wrong_with_CSS
309
310.. note:: Old usage of 'dash' and 'underscore' as the second parameter
311 is DEPRECATED.
Derek Jones8ede1a22011-10-05 13:34:52 -0500312
313The third parameter determines whether or not lowercase characters are
Andrey Andreev08f0f8b2012-11-09 10:27:43 +0200314forced. By default they are not. Options are boolean TRUE/FALSE.
Derek Jones8ede1a22011-10-05 13:34:52 -0500315
Andrey Andreev08f0f8b2012-11-09 10:27:43 +0200316Example::
Derek Jones8ede1a22011-10-05 13:34:52 -0500317
318 $title = "What's wrong with CSS?";
Andrey Andreev08f0f8b2012-11-09 10:27:43 +0200319 $url_title = url_title($title, 'underscore', TRUE);
320 // Produces: whats_wrong_with_css
Derek Jones8ede1a22011-10-05 13:34:52 -0500321
322prep_url()
323----------
324
Andrey Andreev08f0f8b2012-11-09 10:27:43 +0200325.. php:function:: prep_url($str = '')
Derek Jones8ede1a22011-10-05 13:34:52 -0500326
Andrey Andreev08f0f8b2012-11-09 10:27:43 +0200327 :param string $str: URL string
328 :returns: string
Derek Jones8ede1a22011-10-05 13:34:52 -0500329
Andrey Andreev08f0f8b2012-11-09 10:27:43 +0200330This function will add http:// in the event that a protocol prefix
331is missing from a URL.
332
333Pass the URL string to the function like this::
334
335 $url = prep_url('example.com');
Derek Jones8ede1a22011-10-05 13:34:52 -0500336
337redirect()
338==========
339
Andrey Andreev08f0f8b2012-11-09 10:27:43 +0200340.. php:function:: redirect($uri = '', $method = 'auto', $code = NULL)
341
342 :param string $uri: URI string
343 :param string $method: Redirect method ('auto', 'location' or 'refresh')
344 :param string $code: HTTP Response code (usually 302 or 303)
345 :returns: void
346
Derek Jones8ede1a22011-10-05 13:34:52 -0500347Does a "header redirect" to the URI specified. If you specify the full
Brandon Jones50e5dbb2011-11-07 15:51:05 -0500348site URL that link will be built, but for local links simply providing
Derek Jones8ede1a22011-10-05 13:34:52 -0500349the URI segments to the controller you want to direct to will create the
350link. The function will build the URL based on your config file values.
351
Brandon Jones50e5dbb2011-11-07 15:51:05 -0500352The optional second parameter allows you to force a particular redirection
Andrey Andreev08f0f8b2012-11-09 10:27:43 +0200353method. The available methods are **auto**, **location** and **refresh**,
354with location being faster but less reliable on IIS servers.
355The default is **auto**, which will attempt to intelligently choose the
356method based on the server environment.
Derek Jones8ede1a22011-10-05 13:34:52 -0500357
Brandon Jones50e5dbb2011-11-07 15:51:05 -0500358The optional third parameter allows you to send a specific HTTP Response
359Code - this could be used for example to create 301 redirects for search
360engine purposes. The default Response Code is 302. The third parameter is
Andrey Andreev08f0f8b2012-11-09 10:27:43 +0200361*only* available with **location** redirects, and not *refresh*. Examples::
Derek Jones8ede1a22011-10-05 13:34:52 -0500362
363 if ($logged_in == FALSE)
364 {      
Brandon Jones50e5dbb2011-11-07 15:51:05 -0500365 redirect('/login/form/');
Derek Jones8ede1a22011-10-05 13:34:52 -0500366 }
367
368 // with 301 redirect
369 redirect('/article/13', 'location', 301);
370
371.. note:: In order for this function to work it must be used before anything
372 is outputted to the browser since it utilizes server headers.
373
374.. note:: For very fine grained control over headers, you should use the
Andrey Andreev08f0f8b2012-11-09 10:27:43 +0200375 `Output Library </libraries/output>` ``set_header()`` method.
vlakoff530b9462012-09-17 14:18:07 +0200376
Andrey Andreev08f0f8b2012-11-09 10:27:43 +0200377.. note:: To IIS users: if you hide the `Server` HTTP header, the *auto*
vlakoff530b9462012-09-17 14:18:07 +0200378 method won't detect IIS, in that case it is advised you explicitly
Andrey Andreev08f0f8b2012-11-09 10:27:43 +0200379 use the **refresh** method.
380
381.. note:: When the **location** method is used, an HTTP status code of 303
382 will *automatically* be selected when the page is currently accessed
383 via POST and HTTP/1.1 is used.
384
Andrey Andreev1a001492013-01-24 11:32:29 +0200385.. important:: This function will terminate script execution.