blob: 5b8fa5f444002a350119ce322ce0309a341798df [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 Andreev08f0f8b2012-11-09 10:27:43 +020021.. php:function:: site_url($uri = '')
22
23 :param string $uri: URI string
24 :returns: string
25
Derek Jones8ede1a22011-10-05 13:34:52 -050026Returns your site URL, as specified in your config file. The index.php
Andrey Andreev08f0f8b2012-11-09 10:27:43 +020027file (or whatever you have set as your site **index_page** in your config
Derek Jones8ede1a22011-10-05 13:34:52 -050028file) will be added to the URL, as will any URI segments you pass to the
Andrey Andreev08f0f8b2012-11-09 10:27:43 +020029function, plus the **url_suffix** as set in your config file.
Derek Jones8ede1a22011-10-05 13:34:52 -050030
31You are encouraged to use this function any time you need to generate a
32local URL so that your pages become more portable in the event your URL
33changes.
34
35Segments can be optionally passed to the function as a string or an
Andrey Andreev08f0f8b2012-11-09 10:27:43 +020036array. Here is a string example::
Derek Jones8ede1a22011-10-05 13:34:52 -050037
Andrey Andreev08f0f8b2012-11-09 10:27:43 +020038 echo site_url('news/local/123');
Derek Jones8ede1a22011-10-05 13:34:52 -050039
40The above example would return something like:
Andrey Andreev08f0f8b2012-11-09 10:27:43 +020041*http://example.com/index.php/news/local/123*
Derek Jones8ede1a22011-10-05 13:34:52 -050042
Andrey Andreev08f0f8b2012-11-09 10:27:43 +020043Here is an example of segments passed as an array::
Derek Jones8ede1a22011-10-05 13:34:52 -050044
45 $segments = array('news', 'local', '123');
46 echo site_url($segments);
47
Andrey Andreev08f0f8b2012-11-09 10:27:43 +020048This function is an alias for ``CI_Config::site_url()``. For more info,
49please see the :doc:`Config Library <../libraries/config>` documentation.
50
Derek Jones8ede1a22011-10-05 13:34:52 -050051base_url()
52===========
53
Andrey Andreev08f0f8b2012-11-09 10:27:43 +020054.. php:function:: base_url($uri = '')
Derek Jones8ede1a22011-10-05 13:34:52 -050055
Andrey Andreev08f0f8b2012-11-09 10:27:43 +020056 :param string $uri: URI string
57 :returns: string
58
59Returns your site base URL, as specified in your config file. Example::
Derek Jones8ede1a22011-10-05 13:34:52 -050060
61 echo base_url();
62
Andrey Andreev08f0f8b2012-11-09 10:27:43 +020063This function returns the same thing as :php:func:`site_url()`, without
64the *index_page* or *url_suffix* being appended.
Derek Jones8ede1a22011-10-05 13:34:52 -050065
Andrey Andreev08f0f8b2012-11-09 10:27:43 +020066Also like :php:func:`site_url()`, you can supply segments as a string or
67an array. Here is a string example::
Derek Jones8ede1a22011-10-05 13:34:52 -050068
69 echo base_url("blog/post/123");
70
71The above example would return something like:
Andrey Andreev08f0f8b2012-11-09 10:27:43 +020072*http://example.com/blog/post/123*
Derek Jones8ede1a22011-10-05 13:34:52 -050073
Andrey Andreev08f0f8b2012-11-09 10:27:43 +020074This is useful because unlike :php:func:`site_url()`, you can supply a
75string to a file, such as an image or stylesheet. For example::
Derek Jones8ede1a22011-10-05 13:34:52 -050076
77 echo base_url("images/icons/edit.png");
78
79This would give you something like:
Andrey Andreev08f0f8b2012-11-09 10:27:43 +020080*http://example.com/images/icons/edit.png*
81
82This function is an alias for ``CI_Config::base_url()``. For more info,
83please see the :doc:`Config Library <../libraries/config>` documentation.
Derek Jones8ede1a22011-10-05 13:34:52 -050084
85current_url()
86=============
87
Andrey Andreev08f0f8b2012-11-09 10:27:43 +020088.. php:function:: current_url()
89
90 :returns: string
91
Derek Jones8ede1a22011-10-05 13:34:52 -050092Returns the full URL (including segments) of the page being currently
93viewed.
94
Andrey Andreev08f0f8b2012-11-09 10:27:43 +020095.. note:: Calling this function is the same as doing this:
96 |
97 | site_url(uri_string());
98
Derek Jones8ede1a22011-10-05 13:34:52 -050099uri_string()
100============
101
Andrey Andreev08f0f8b2012-11-09 10:27:43 +0200102.. php:function:: uri_string()
Derek Jones8ede1a22011-10-05 13:34:52 -0500103
Andrey Andreev08f0f8b2012-11-09 10:27:43 +0200104 :returns: string
105
106Returns the URI segments of any page that contains this function.
107For example, if your URL was this::
Derek Jones8ede1a22011-10-05 13:34:52 -0500108
109 http://some-site.com/blog/comments/123
110
Andrey Andreev08f0f8b2012-11-09 10:27:43 +0200111The function would return::
Derek Jones8ede1a22011-10-05 13:34:52 -0500112
113 /blog/comments/123
114
Andrey Andreev08f0f8b2012-11-09 10:27:43 +0200115This function is an alias for ``CI_Config::uri_string()``. For more info,
116please see the :doc:`Config Library <../libraries/config>` documentation.
117
Derek Jones8ede1a22011-10-05 13:34:52 -0500118index_page()
119============
120
Andrey Andreev08f0f8b2012-11-09 10:27:43 +0200121.. php:function:: index_page()
Derek Jones8ede1a22011-10-05 13:34:52 -0500122
Andrey Andreev08f0f8b2012-11-09 10:27:43 +0200123 :returns: string
124
125Returns your site **index_page**, as specified in your config file.
126Example::
Derek Jones8ede1a22011-10-05 13:34:52 -0500127
128 echo index_page();
129
130anchor()
131========
132
Andrey Andreev08f0f8b2012-11-09 10:27:43 +0200133.. php:function:: anchor($uri = '', $title = '', $attributes = '')
Derek Jones8ede1a22011-10-05 13:34:52 -0500134
Andrey Andreev08f0f8b2012-11-09 10:27:43 +0200135 :param string $uri: URI string
136 :param string $title: Anchor title
137 :param mixed $attributes: HTML attributes
138 :returns: string
Derek Jones8ede1a22011-10-05 13:34:52 -0500139
Andrey Andreev08f0f8b2012-11-09 10:27:43 +0200140Creates a standard HTML anchor link based on your local site URL.
Derek Jones8ede1a22011-10-05 13:34:52 -0500141
142The first parameter can contain any segments you wish appended to the
Andrey Andreev08f0f8b2012-11-09 10:27:43 +0200143URL. As with the :php:func:`site_url()` function above, segments can
144be a string or an array.
Derek Jones8ede1a22011-10-05 13:34:52 -0500145
146.. note:: If you are building links that are internal to your application
Andrey Andreev08f0f8b2012-11-09 10:27:43 +0200147 do not include the base URL (http://...). This will be added
148 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
151The second segment is the text you would like the link to say. If you
152leave it blank, the URL will be used.
153
154The third parameter can contain a list of attributes you would like
155added to the link. The attributes can be a simple string or an
156associative array.
157
Andrey Andreev08f0f8b2012-11-09 10:27:43 +0200158Here are some examples::
Derek Jones8ede1a22011-10-05 13:34:52 -0500159
160 echo anchor('news/local/123', 'My News', 'title="News title"');
Andrey Andreev08f0f8b2012-11-09 10:27:43 +0200161 // 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
163 echo anchor('news/local/123', 'My News', array('title' => 'The best news!'));
Andrey Andreev08f0f8b2012-11-09 10:27:43 +0200164 // 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
Andrey Andreev08f0f8b2012-11-09 10:27:43 +0200166 echo anchor('', 'Click here');
167 // Prints: <a href="http://example.com">Click Here</a>
Derek Jones8ede1a22011-10-05 13:34:52 -0500168
169anchor_popup()
170==============
171
Andrey Andreev08f0f8b2012-11-09 10:27:43 +0200172.. php:function:: anchor_popup($uri = '', $title = '', $attributes = FALSE)
Derek Jones8ede1a22011-10-05 13:34:52 -0500173
Andrey Andreev08f0f8b2012-11-09 10:27:43 +0200174 :param string $uri: URI string
175 :param string $title: Anchor title
176 :param mixed $attributes: HTML attributes
177 :returns: string
178
179Nearly identical to the :php:func:``anchor()`` function except that it
180opens the URL in a new window. You can specify JavaScript window
181attributes in the third parameter to control how the window is opened.
182If the third parameter is not set it will simply open a new window with
183your own browser settings.
184
185Here is an example with attributes::
Derek Jones8ede1a22011-10-05 13:34:52 -0500186
Andrey Andreev81c32082012-06-16 21:21:46 +0300187 $atts = array(
Andrey Andreev08f0f8b2012-11-09 10:27:43 +0200188 'width' => 800,
189 'height' => 600,
Andrey Andreev81c32082012-06-16 21:21:46 +0300190 'scrollbars' => 'yes',
191 'status'      => 'yes',
192 'resizable'   => 'yes',
Andrey Andreev08f0f8b2012-11-09 10:27:43 +0200193 'screenx' => 0,
194 'screeny' => 0,
Andrey Andreev81c32082012-06-16 21:21:46 +0300195 'window_name' => '_blank'
Derek Jones8ede1a22011-10-05 13:34:52 -0500196 );
197
198 echo anchor_popup('news/local/123', 'Click Me!', $atts);
199
Andrey Andreev81c32082012-06-16 21:21:46 +0300200.. note:: The above attributes are the function defaults so you only need to
Derek Jonesce79be02012-06-25 23:23:46 -0700201 set the ones that are different from what you need. If you want the
202 function to use all of its defaults simply pass an empty array in the
Andrey Andreev08f0f8b2012-11-09 10:27:43 +0200203 third parameter:
204 |
205 | echo anchor_popup('news/local/123', 'Click Me!', array());
Derek Jones8ede1a22011-10-05 13:34:52 -0500206
Andrey Andreev08f0f8b2012-11-09 10:27:43 +0200207.. note:: The **window_name** is not really an attribute, but an argument to
Andrey Andreev81c32082012-06-16 21:21:46 +0300208 the JavaScript `window.open() <http://www.w3schools.com/jsref/met_win_open.asp>`
209 method, which accepts either a window name or a window target.
210
211.. note:: Any other attribute than the listed above will be parsed as an
212 HTML attribute to the anchor tag.
213
Derek Jones8ede1a22011-10-05 13:34:52 -0500214mailto()
215========
216
Andrey Andreev08f0f8b2012-11-09 10:27:43 +0200217.. php:function:: mailto($email, $title = '', $attributes = '')
Derek Jones8ede1a22011-10-05 13:34:52 -0500218
Andrey Andreev08f0f8b2012-11-09 10:27:43 +0200219 :param string $email: E-mail address
220 :param string $title: Anchor title
221 :param mixed $attributes: HTML attributes
222 :returns: string
223
224Creates a standard HTML e-mail link. Usage example::
Derek Jones8ede1a22011-10-05 13:34:52 -0500225
226 echo mailto('me@my-site.com', 'Click Here to Contact Me');
227
Andrey Andreev08f0f8b2012-11-09 10:27:43 +0200228As with the :php:func:`anchor()` tab above, you can set attributes using the
229third parameter::
InFog00b3df42012-07-29 13:42:50 -0300230
Andrey Andreev08f0f8b2012-11-09 10:27:43 +0200231 $attributes = array('title' => 'Mail me');
232 echo mailto('me@my-site.com', 'Contact Me', $attributes);
Derek Jones8ede1a22011-10-05 13:34:52 -0500233
234safe_mailto()
235=============
236
Andrey Andreev08f0f8b2012-11-09 10:27:43 +0200237.. php:function:: safe_mailto($email, $title = '', $attributes = '')
238
239 :param string $email: E-mail address
240 :param string $title: Anchor title
241 :param mixed $attributes: HTML attributes
242 :returns: string
243
244Identical to the :php:func:`mailto()` function except it writes an obfuscated
245version of the *mailto* tag using ordinal numbers written with JavaScript to
246help prevent the e-mail address from being harvested by spam bots.
Derek Jones8ede1a22011-10-05 13:34:52 -0500247
248auto_link()
249===========
250
Andrey Andreev08f0f8b2012-11-09 10:27:43 +0200251.. php:function:: auto_link($str, $type = 'both', $popup = FALSE)
Derek Jones8ede1a22011-10-05 13:34:52 -0500252
Andrey Andreev08f0f8b2012-11-09 10:27:43 +0200253 :param string $str: Input string
254 :param string $type: Link type ('email', 'url' or 'both')
255 :param bool $popup: Whether to create popup links
256 :returns: string
257
258Automatically turns URLs and e-mail addresses contained in a string into
259links. Example::
Derek Jones8ede1a22011-10-05 13:34:52 -0500260
261 $string = auto_link($string);
262
Andrey Andreev08f0f8b2012-11-09 10:27:43 +0200263The second parameter determines whether URLs and e-mails are converted or
Derek Jones8ede1a22011-10-05 13:34:52 -0500264just one or the other. Default behavior is both if the parameter is not
Andrey Andreev08f0f8b2012-11-09 10:27:43 +0200265specified. E-mail links are encoded as :php:func:`safe_mailto()` as shown
266above.
Derek Jones8ede1a22011-10-05 13:34:52 -0500267
Andrey Andreev08f0f8b2012-11-09 10:27:43 +0200268Converts only URLs::
Derek Jones8ede1a22011-10-05 13:34:52 -0500269
270 $string = auto_link($string, 'url');
271
Andrey Andreev08f0f8b2012-11-09 10:27:43 +0200272Converts only e-mail addresses::
Derek Jones8ede1a22011-10-05 13:34:52 -0500273
274 $string = auto_link($string, 'email');
275
276The third parameter determines whether links are shown in a new window.
Andrey Andreev08f0f8b2012-11-09 10:27:43 +0200277The value can be TRUE or FALSE (boolean)::
Derek Jones8ede1a22011-10-05 13:34:52 -0500278
279 $string = auto_link($string, 'both', TRUE);
280
281url_title()
282===========
283
Andrey Andreev08f0f8b2012-11-09 10:27:43 +0200284.. php:function:: url_title($str, $separator = '-', $lowercase = FALSE)
285
286 :param string $str: Input string
287 :param string $separator: Word separator
288 :param string $lowercase: Whether to transform the output string to lower-case
289 :returns: string
290
Derek Jones8ede1a22011-10-05 13:34:52 -0500291Takes a string as input and creates a human-friendly URL string. This is
292useful if, for example, you have a blog in which you'd like to use the
Andrey Andreev08f0f8b2012-11-09 10:27:43 +0200293title of your entries in the URL. Example::
Derek Jones8ede1a22011-10-05 13:34:52 -0500294
295 $title = "What's wrong with CSS?";
Andrey Andreev08f0f8b2012-11-09 10:27:43 +0200296 $url_title = url_title($title);
297 // Produces: Whats-wrong-with-CSS
Derek Jones8ede1a22011-10-05 13:34:52 -0500298
299The second parameter determines the word delimiter. By default dashes
Andrey Andreev08f0f8b2012-11-09 10:27:43 +0200300are used. Preferred options are: **-** (dash) or **_** (underscore)
Derek Jones8ede1a22011-10-05 13:34:52 -0500301
Andrey Andreev08f0f8b2012-11-09 10:27:43 +0200302Example::
Derek Jones8ede1a22011-10-05 13:34:52 -0500303
304 $title = "What's wrong with CSS?";
Andrey Andreev08f0f8b2012-11-09 10:27:43 +0200305 $url_title = url_title($title, 'underscore');
306 // Produces: Whats_wrong_with_CSS
307
308.. note:: Old usage of 'dash' and 'underscore' as the second parameter
309 is DEPRECATED.
Derek Jones8ede1a22011-10-05 13:34:52 -0500310
311The third parameter determines whether or not lowercase characters are
Andrey Andreev08f0f8b2012-11-09 10:27:43 +0200312forced. By default they are not. Options are boolean TRUE/FALSE.
Derek Jones8ede1a22011-10-05 13:34:52 -0500313
Andrey Andreev08f0f8b2012-11-09 10:27:43 +0200314Example::
Derek Jones8ede1a22011-10-05 13:34:52 -0500315
316 $title = "What's wrong with CSS?";
Andrey Andreev08f0f8b2012-11-09 10:27:43 +0200317 $url_title = url_title($title, 'underscore', TRUE);
318 // Produces: whats_wrong_with_css
Derek Jones8ede1a22011-10-05 13:34:52 -0500319
320prep_url()
321----------
322
Andrey Andreev08f0f8b2012-11-09 10:27:43 +0200323.. php:function:: prep_url($str = '')
Derek Jones8ede1a22011-10-05 13:34:52 -0500324
Andrey Andreev08f0f8b2012-11-09 10:27:43 +0200325 :param string $str: URL string
326 :returns: string
Derek Jones8ede1a22011-10-05 13:34:52 -0500327
Andrey Andreev08f0f8b2012-11-09 10:27:43 +0200328This function will add http:// in the event that a protocol prefix
329is missing from a URL.
330
331Pass the URL string to the function like this::
332
333 $url = prep_url('example.com');
Derek Jones8ede1a22011-10-05 13:34:52 -0500334
335redirect()
336==========
337
Andrey Andreev08f0f8b2012-11-09 10:27:43 +0200338.. php:function:: redirect($uri = '', $method = 'auto', $code = NULL)
339
340 :param string $uri: URI string
341 :param string $method: Redirect method ('auto', 'location' or 'refresh')
342 :param string $code: HTTP Response code (usually 302 or 303)
343 :returns: void
344
Derek Jones8ede1a22011-10-05 13:34:52 -0500345Does a "header redirect" to the URI specified. If you specify the full
Brandon Jones50e5dbb2011-11-07 15:51:05 -0500346site URL that link will be built, but for local links simply providing
Derek Jones8ede1a22011-10-05 13:34:52 -0500347the URI segments to the controller you want to direct to will create the
348link. The function will build the URL based on your config file values.
349
Brandon Jones50e5dbb2011-11-07 15:51:05 -0500350The optional second parameter allows you to force a particular redirection
Andrey Andreev08f0f8b2012-11-09 10:27:43 +0200351method. The available methods are **auto**, **location** and **refresh**,
352with location being faster but less reliable on IIS servers.
353The default is **auto**, which will attempt to intelligently choose the
354method based on the server environment.
Derek Jones8ede1a22011-10-05 13:34:52 -0500355
Brandon Jones50e5dbb2011-11-07 15:51:05 -0500356The optional third parameter allows you to send a specific HTTP Response
357Code - this could be used for example to create 301 redirects for search
358engine purposes. The default Response Code is 302. The third parameter is
Andrey Andreev08f0f8b2012-11-09 10:27:43 +0200359*only* available with **location** redirects, and not *refresh*. Examples::
Derek Jones8ede1a22011-10-05 13:34:52 -0500360
361 if ($logged_in == FALSE)
362 {      
Brandon Jones50e5dbb2011-11-07 15:51:05 -0500363 redirect('/login/form/');
Derek Jones8ede1a22011-10-05 13:34:52 -0500364 }
365
366 // with 301 redirect
367 redirect('/article/13', 'location', 301);
368
369.. note:: In order for this function to work it must be used before anything
370 is outputted to the browser since it utilizes server headers.
371
372.. note:: For very fine grained control over headers, you should use the
Andrey Andreev08f0f8b2012-11-09 10:27:43 +0200373 `Output Library </libraries/output>` ``set_header()`` method.
vlakoff530b9462012-09-17 14:18:07 +0200374
Andrey Andreev08f0f8b2012-11-09 10:27:43 +0200375.. note:: To IIS users: if you hide the `Server` HTTP header, the *auto*
vlakoff530b9462012-09-17 14:18:07 +0200376 method won't detect IIS, in that case it is advised you explicitly
Andrey Andreev08f0f8b2012-11-09 10:27:43 +0200377 use the **refresh** method.
378
379.. note:: When the **location** method is used, an HTTP status code of 303
380 will *automatically* be selected when the page is currently accessed
381 via POST and HTTP/1.1 is used.
382
383.. important:: This function will terminate script execution.