blob: e5e35dfdd5e6092d9a9194b82cbf2aabfe4cbef5 [file] [log] [blame]
adminb0dd10f2006-08-25 17:25:49 +00001<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
Derek Allardafd99ac2008-01-19 19:59:14 +00002<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
adminb0dd10f2006-08-25 17:25:49 +00003<head>
4
Derek Allard8039d4c2008-05-31 02:47:56 +00005<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
Derek Jonesfd93d222008-05-06 15:18:50 +00006<title>URL Helper : CodeIgniter User Guide</title>
adminb0dd10f2006-08-25 17:25:49 +00007
8<style type='text/css' media='all'>@import url('../userguide.css');</style>
9<link rel='stylesheet' type='text/css' media='all' href='../userguide.css' />
10
admin17a890d2006-09-27 20:42:42 +000011<script type="text/javascript" src="../nav/nav.js"></script>
admin2296fc32006-09-27 21:07:02 +000012<script type="text/javascript" src="../nav/prototype.lite.js"></script>
admin17a890d2006-09-27 20:42:42 +000013<script type="text/javascript" src="../nav/moo.fx.js"></script>
Derek Allardb3412372007-10-25 12:15:16 +000014<script type="text/javascript" src="../nav/user_guide_menu.js"></script>
adminb0dd10f2006-08-25 17:25:49 +000015
adminb0dd10f2006-08-25 17:25:49 +000016<meta http-equiv='expires' content='-1' />
17<meta http-equiv= 'pragma' content='no-cache' />
18<meta name='robots' content='all' />
Derek Allard3d879d52008-01-18 19:41:32 +000019<meta name='author' content='ExpressionEngine Dev Team' />
Derek Allardd2df9bc2007-04-15 17:41:17 +000020<meta name='description' content='CodeIgniter User Guide' />
adminb0dd10f2006-08-25 17:25:49 +000021</head>
22<body>
23
24<!-- START NAVIGATION -->
25<div id="nav"><div id="nav_inner"><script type="text/javascript">create_menu('../');</script></div></div>
Rick Ellis7cdef032008-08-26 18:44:54 +000026<div id="nav2"><a name="top"></a><a href="javascript:void(0);" onclick="myHeight.toggle();"><img src="../images/nav_toggle_darker.jpg" width="154" height="43" border="0" title="Toggle Table of Contents" alt="Toggle Table of Contents" /></a></div>
adminb0dd10f2006-08-25 17:25:49 +000027<div id="masthead">
28<table cellpadding="0" cellspacing="0" border="0" style="width:100%">
29<tr>
Rick Ellis25949532008-08-26 19:48:08 +000030<td><h1>CodeIgniter User Guide Version 1.7</h1></td>
adminc0d5d522006-10-30 19:40:35 +000031<td id="breadcrumb_right"><a href="../toc.html">Table of Contents Page</a></td>
adminb0dd10f2006-08-25 17:25:49 +000032</tr>
33</table>
34</div>
35<!-- END NAVIGATION -->
36
37
38<!-- START BREADCRUMB -->
39<table cellpadding="0" cellspacing="0" border="0" style="width:100%">
40<tr>
41<td id="breadcrumb">
Derek Jones7a9193a2008-01-21 18:39:20 +000042<a href="http://codeigniter.com/">CodeIgniter Home</a> &nbsp;&#8250;&nbsp;
adminb0dd10f2006-08-25 17:25:49 +000043<a href="../index.html">User Guide Home</a> &nbsp;&#8250;&nbsp;
44URL Helper
45</td>
Derek Allardbc030912007-06-24 18:25:29 +000046<td id="searchbox"><form method="get" action="http://www.google.com/search"><input type="hidden" name="as_sitesearch" id="as_sitesearch" value="codeigniter.com/user_guide/" />Search User Guide&nbsp; <input type="text" class="input" style="width:200px;" name="q" id="q" size="31" maxlength="255" value="" />&nbsp;<input type="submit" class="submit" name="sa" value="Go" /></form></td>
adminb0dd10f2006-08-25 17:25:49 +000047</tr>
48</table>
49<!-- END BREADCRUMB -->
50
51<br clear="all" />
52
53
54<!-- START CONTENT -->
55<div id="content">
56
57
58<h1>URL Helper</h1>
59
60<p>The URL Helper file contains functions that assist in working with URLs.</p>
61
62
63<h2>Loading this Helper</h2>
64
65<p>This helper is loaded using the following code:</p>
66<code>$this->load->helper('url');</code>
67
68<p>The following functions are available:</p>
69
70<h2>site_url()</h2>
71
72<p>Returns your site URL, as specified in your config file. The index.php file (or whatever you have set as your
73site <dfn>index_page</dfn> in your config file) will be added to the URL, as will any URI segments you pass to the function.</p>
74
admine334c472006-10-21 19:44:22 +000075<p>You are encouraged to use this function any time you need to generate a local URL so that your pages become more portable
adminb0dd10f2006-08-25 17:25:49 +000076in the event your URL changes.</p>
77
78<p>Segments can be optionally passed to the function as a string or an array. Here is a string example:</p>
79
80<code>echo site_url("news/local/123");</code>
81
Derek Jonesff845f92008-06-26 17:05:55 +000082<p>The above example would return something like: http://example.com/index.php/news/local/123</p>
adminb0dd10f2006-08-25 17:25:49 +000083
84<p>Here is an example of segments passed as an array:</p>
85
86<code>
87$segments = array('news', 'local', '123');<br />
88<br />
89echo site_url($segments);</code>
90
91
92<h2>base_url()</h2>
93<p>Returns your site base URL, as specified in your config file. Example:</p>
94<code>echo base_url();</code>
95
96
Rick Ellis587b37a2008-08-19 22:16:40 +000097<h2>current_url()</h2>
98<p>Returns the full URL (including segments) of the page being currently viewed.</p>
99
100
101<h2>uri_segments()</h2>
102<p>Returns the URI segments of any page that contains this function. For example, if your URL was this:</p>
103<code>http://some-site.com/blog/comments/123</code>
104
105<p>The function would return:</p>
106<code>blog/comments/123</code>
107
108
adminb0dd10f2006-08-25 17:25:49 +0000109<h2>index_page()</h2>
110<p>Returns your site "index" page, as specified in your config file. Example:</p>
111<code>echo index_page();</code>
112
113
114
115<h2>anchor()</h2>
116
117<p>Creates a standard HTML anchor link based on your local site URL:</p>
118
Derek Jonesff845f92008-06-26 17:05:55 +0000119<code>&lt;a href="http://example.com">Click Here&lt;/a></code>
adminb0dd10f2006-08-25 17:25:49 +0000120
121<p>The tag has three optional parameters:</p>
122
123<code>anchor(<var>uri segments</var>, <var>text</var>, <var>attributes</var>)</code>
124
125<p>The first parameter can contain any segments you wish appended to the URL. As with the <dfn>site_url()</dfn> function above,
adminb071bb52006-08-26 19:28:37 +0000126segments can be a string or an array.</p>
127
128<p><strong>Note:</strong>&nbsp; If you are building links that are internal to your application do not include the base URL (http://...). This
129will be added automatically from the information specified in your config file. Include only the URI segments you wish appended to the URL.</p>
adminb0dd10f2006-08-25 17:25:49 +0000130
131<p>The second segment is the text you would like the link to say. If you leave it blank, the URL will be used.</p>
132
133<p>The third parameter can contain a list of attributes you would like added to the link. The attributes can be a simple string or an associative array.</p>
134
135<p>Here are some examples:</p>
136
Derek Allardc4e9dc92007-02-06 19:52:30 +0000137<code>echo anchor('news/local/123', 'My News');</code>
adminb0dd10f2006-08-25 17:25:49 +0000138
Derek Jonesff845f92008-06-26 17:05:55 +0000139<p>Would produce: &lt;a href="http://example.com/index.php/news/local/123" title="My News">My News&lt;/a></p>
adminb0dd10f2006-08-25 17:25:49 +0000140
Derek Allardc4e9dc92007-02-06 19:52:30 +0000141<code>echo anchor('news/local/123', 'My News', array('title' => 'The best news!'));</code>
adminb0dd10f2006-08-25 17:25:49 +0000142
Derek Jonesff845f92008-06-26 17:05:55 +0000143<p>Would produce: &lt;a href="http://example.com/index.php/news/local/123" title="The best news!">My News&lt;/a></p>
adminb0dd10f2006-08-25 17:25:49 +0000144
145
146<h2>anchor_popup()</h2>
147
admine334c472006-10-21 19:44:22 +0000148<p>Nearly identical to the <dfn>anchor()</dfn> function except that it opens the URL in a new window.
adminb0dd10f2006-08-25 17:25:49 +0000149
admine334c472006-10-21 19:44:22 +0000150You can specify JavaScript window attributes in the third parameter to control how the window is opened. If
adminb0dd10f2006-08-25 17:25:49 +0000151the third parameter is not set it will simply open a new window with your own browser settings. Here is an example
152with attributes:</p>
153
154<code>
155
156$atts = array(<br />
157&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'width'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=> '800',<br />
158&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'height'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=> '600',<br />
159&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'scrollbars' => 'yes',<br />
160&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'status'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=> 'yes',<br />
161&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'resizable'&nbsp;&nbsp;=> 'yes',<br />
162&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'screenx'&nbsp;&nbsp;&nbsp;&nbsp;=> '0',<br />
163&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'screeny'&nbsp;&nbsp;&nbsp;&nbsp;=> '0'<br />
164&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;);<br />
165<br />
Derek Allard9967ab42008-02-13 13:14:47 +0000166echo anchor_popup('news/local/123', 'Click Me!', $atts);</code>
adminb0dd10f2006-08-25 17:25:49 +0000167
168<p>Note: The above attributes are the function defaults so you only need to set the ones that are different from what you need.
169If you want the function to use all of its defaults simply pass an empty array in the third parameter:</p>
170
Derek Allardc4e9dc92007-02-06 19:52:30 +0000171<code>echo anchor_popup('news/local/123', 'Click Me!', array());</code>
adminb0dd10f2006-08-25 17:25:49 +0000172
173
174<h2>mailto()</h2>
175
176<p>Creates a standard HTML email link. Usage example:</p>
177
178<code>echo mailto('me@my-site.com', 'Click Here to Contact Me');</code>
179
180<p>As with the <dfn>anchor()</dfn> tab above, you can set attributes using the third parameter.</p>
181
182
183<h2>safe_mailto()</h2>
184
185<p>Identical to the above function except it writes an obfuscated version of the mailto tag using ordinal numbers
186written with JavaScript to help prevent the email address from being harvested by spam bots.</p>
187
188
189<h2>auto_link()</h2>
190
191<p>Automatically turns URLs and email addresses contained in a string into links. Example:</p>
192
193<code>$string = auto_link($string);</code>
194
195<p>The second parameter determines whether URLs and emails are converted or just one or the other. Default behavior is both
Derek Allarddf485332008-05-08 20:41:57 +0000196if the parameter is not specified. Email links are encoded as safe_mailto() as shown above.</p>
adminb0dd10f2006-08-25 17:25:49 +0000197
198<p>Converts only URLs:</p>
199<code>$string = auto_link($string, 'url');</code>
200
201<p>Converts only Email addresses:</p>
202<code>$string = auto_link($string, 'email');</code>
203
Derek Allardce66d152008-03-04 20:55:55 +0000204<p>The third parameter determines whether links are shown in a new window. The value can be TRUE or FALSE (boolean):</p>
adminb0dd10f2006-08-25 17:25:49 +0000205<code>$string = auto_link($string, 'both', TRUE);</code>
206
207
208<h2>url_title()</h2>
209<p>Takes a string as input and creates a human-friendly URL string. This is useful if, for example, you have a blog
210in which you'd like to use the title of your entries in the URL. Example:</p>
211
212<code>$title = "What's wrong with CSS?";<br />
213<br />
214$url_title = url_title($title);<br />
215<br />
216// Produces: whats-wrong-with-css
217</code>
218
219
220<p>The second parameter determines the word delimiter. By default dashes are used. Options are: <dfn>dash</dfn>, or <dfn>underscore</dfn>:</p>
221
222<code>$title = "What's wrong with CSS?";<br />
223<br />
224$url_title = url_title($title, 'underscore');<br />
225<br />
226// Produces: whats_wrong_with_css
227</code>
228
229
230<h3>prep_url()</h3>
231<p>This function will add <kbd>http://</kbd> in the event it is missing from a URL. Pass the URL string to the function like this:</p>
232<code>
Derek Jonesff845f92008-06-26 17:05:55 +0000233$url = "example.com";<br /><br />
adminb0dd10f2006-08-25 17:25:49 +0000234$url = prep_url($url);</code>
235
236
237
238
239<h2>redirect()</h2>
240
241<p>Does a "header redirect" to the local URI specified. Just like other functions in this helper, this one is designed
242to redirect to a local URL within your site. You will <strong>not</strong> specify the full site URL, but rather simply the URI segments
243to the controller you want to direct to. The function will build the URL based on your config file values.</p>
244
Derek Allard05f830c2008-04-27 13:35:20 +0000245<p>The optional second parameter allows you to choose between the "location"
246method (default) or the "refresh" method. Location is faster, but on Windows servers it can sometimes be a problem. The optional third parameter allows you to send a specific HTTP Response Code - this could be used for example to create 301 redirects for search engine purposes. The default Response Code is 302. The third parameter is <em>only</em> available with 'location' redirects, and not 'refresh'. Examples:</p>
adminb0dd10f2006-08-25 17:25:49 +0000247
248<code>if ($logged_in == FALSE)<br />
249{<br />
250&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;redirect('/login/form/', 'refresh');<br />
Derek Allard05f830c2008-04-27 13:35:20 +0000251}<br />
252<br />
253// with 301 redirect<br />
254redirect('/article/13', 'location', 301);</code>
adminb0dd10f2006-08-25 17:25:49 +0000255
256<p class="important"><strong>Note:</strong> In order for this function to work it must be used before anything is outputted
Derek Allard05f830c2008-04-27 13:35:20 +0000257to the browser since it utilizes server headers.<br />
258<strong>Note:</strong> For very fine grained control over headers, you should use the <a href="../libraries/output.html">Output Library</a>'s set_header() function.</p>
adminb0dd10f2006-08-25 17:25:49 +0000259
260
261
262
263
264
265</div>
266<!-- END CONTENT -->
267
268
269<div id="footer">
270<p>
271Previous Topic:&nbsp;&nbsp;<a href="typography_helper.html">Typography Helper</a>
272&nbsp;&nbsp;&nbsp;&middot;&nbsp;&nbsp;
273<a href="#top">Top of Page</a>&nbsp;&nbsp;&nbsp;&middot;&nbsp;&nbsp;
274<a href="../index.html">User Guide Home</a>&nbsp;&nbsp;&nbsp;&middot;&nbsp;&nbsp;
275Next Topic:&nbsp;&nbsp;<a href="xml_helper.html">XML Helper</a>
Derek Allardc6441282007-07-04 23:54:32 +0000276</p>
Derek Jones07870432008-02-13 03:49:26 +0000277<p><a href="http://codeigniter.com">CodeIgniter</a> &nbsp;&middot;&nbsp; Copyright &#169; 2006-2008 &nbsp;&middot;&nbsp; <a href="http://ellislab.com/">Ellislab, Inc.</a></p>
adminb0dd10f2006-08-25 17:25:49 +0000278</div>
279
280</body>
281</html>