Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 1 | ########## |
| 2 | URL Helper |
| 3 | ########## |
| 4 | |
| 5 | The URL Helper file contains functions that assist in working with URLs. |
| 6 | |
| 7 | .. contents:: Page Contents |
| 8 | |
| 9 | Loading this Helper |
| 10 | =================== |
| 11 | |
Andrey Andreev | 08f0f8b | 2012-11-09 10:27:43 +0200 | [diff] [blame] | 12 | This helper is loaded using the following code:: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 13 | |
| 14 | $this->load->helper('url'); |
| 15 | |
| 16 | The following functions are available: |
| 17 | |
| 18 | site_url() |
| 19 | ========== |
| 20 | |
Andrey Andreev | 2023c3d | 2013-07-18 03:19:59 +0300 | [diff] [blame] | 21 | .. php:function:: site_url($uri = '', $protocol = NULL) |
Andrey Andreev | 08f0f8b | 2012-11-09 10:27:43 +0200 | [diff] [blame] | 22 | |
| 23 | :param string $uri: URI string |
Andrey Andreev | 2023c3d | 2013-07-18 03:19:59 +0300 | [diff] [blame] | 24 | :param string $protocol: Protocol, e.g. 'http' or 'https' |
Andrey Andreev | 08f0f8b | 2012-11-09 10:27:43 +0200 | [diff] [blame] | 25 | :returns: string |
| 26 | |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 27 | Returns your site URL, as specified in your config file. The index.php |
Andrey Andreev | 08f0f8b | 2012-11-09 10:27:43 +0200 | [diff] [blame] | 28 | file (or whatever you have set as your site **index_page** in your config |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 29 | file) will be added to the URL, as will any URI segments you pass to the |
Andrey Andreev | 08f0f8b | 2012-11-09 10:27:43 +0200 | [diff] [blame] | 30 | function, plus the **url_suffix** as set in your config file. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 31 | |
| 32 | You are encouraged to use this function any time you need to generate a |
| 33 | local URL so that your pages become more portable in the event your URL |
| 34 | changes. |
| 35 | |
| 36 | Segments can be optionally passed to the function as a string or an |
Andrey Andreev | 08f0f8b | 2012-11-09 10:27:43 +0200 | [diff] [blame] | 37 | array. Here is a string example:: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 38 | |
Andrey Andreev | 08f0f8b | 2012-11-09 10:27:43 +0200 | [diff] [blame] | 39 | echo site_url('news/local/123'); |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 40 | |
| 41 | The above example would return something like: |
Andrey Andreev | 08f0f8b | 2012-11-09 10:27:43 +0200 | [diff] [blame] | 42 | *http://example.com/index.php/news/local/123* |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 43 | |
Andrey Andreev | 08f0f8b | 2012-11-09 10:27:43 +0200 | [diff] [blame] | 44 | Here is an example of segments passed as an array:: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 45 | |
| 46 | $segments = array('news', 'local', '123'); |
| 47 | echo site_url($segments); |
| 48 | |
Andrey Andreev | 08f0f8b | 2012-11-09 10:27:43 +0200 | [diff] [blame] | 49 | This function is an alias for ``CI_Config::site_url()``. For more info, |
| 50 | please see the :doc:`Config Library <../libraries/config>` documentation. |
| 51 | |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 52 | base_url() |
| 53 | =========== |
| 54 | |
Andrey Andreev | 2023c3d | 2013-07-18 03:19:59 +0300 | [diff] [blame] | 55 | .. php:function:: base_url($uri = '', $protocol = NULL) |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 56 | |
Andrey Andreev | 08f0f8b | 2012-11-09 10:27:43 +0200 | [diff] [blame] | 57 | :param string $uri: URI string |
Andrey Andreev | 2023c3d | 2013-07-18 03:19:59 +0300 | [diff] [blame] | 58 | :param string $protocol: Protocol, e.g. 'http' or 'https' |
Andrey Andreev | 08f0f8b | 2012-11-09 10:27:43 +0200 | [diff] [blame] | 59 | :returns: string |
| 60 | |
| 61 | Returns your site base URL, as specified in your config file. Example:: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 62 | |
| 63 | echo base_url(); |
| 64 | |
Andrey Andreev | 08f0f8b | 2012-11-09 10:27:43 +0200 | [diff] [blame] | 65 | This function returns the same thing as :php:func:`site_url()`, without |
| 66 | the *index_page* or *url_suffix* being appended. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 67 | |
Andrey Andreev | 08f0f8b | 2012-11-09 10:27:43 +0200 | [diff] [blame] | 68 | Also like :php:func:`site_url()`, you can supply segments as a string or |
| 69 | an array. Here is a string example:: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 70 | |
| 71 | echo base_url("blog/post/123"); |
| 72 | |
| 73 | The above example would return something like: |
Andrey Andreev | 08f0f8b | 2012-11-09 10:27:43 +0200 | [diff] [blame] | 74 | *http://example.com/blog/post/123* |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 75 | |
Andrey Andreev | 08f0f8b | 2012-11-09 10:27:43 +0200 | [diff] [blame] | 76 | This is useful because unlike :php:func:`site_url()`, you can supply a |
| 77 | string to a file, such as an image or stylesheet. For example:: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 78 | |
| 79 | echo base_url("images/icons/edit.png"); |
| 80 | |
| 81 | This would give you something like: |
Andrey Andreev | 08f0f8b | 2012-11-09 10:27:43 +0200 | [diff] [blame] | 82 | *http://example.com/images/icons/edit.png* |
| 83 | |
| 84 | This function is an alias for ``CI_Config::base_url()``. For more info, |
| 85 | please see the :doc:`Config Library <../libraries/config>` documentation. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 86 | |
| 87 | current_url() |
| 88 | ============= |
| 89 | |
Andrey Andreev | 08f0f8b | 2012-11-09 10:27:43 +0200 | [diff] [blame] | 90 | .. php:function:: current_url() |
| 91 | |
| 92 | :returns: string |
| 93 | |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 94 | Returns the full URL (including segments) of the page being currently |
| 95 | viewed. |
| 96 | |
Andrey Andreev | 08f0f8b | 2012-11-09 10:27:43 +0200 | [diff] [blame] | 97 | .. note:: Calling this function is the same as doing this: |
| 98 | | |
| 99 | | site_url(uri_string()); |
| 100 | |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 101 | uri_string() |
| 102 | ============ |
| 103 | |
Andrey Andreev | 08f0f8b | 2012-11-09 10:27:43 +0200 | [diff] [blame] | 104 | .. php:function:: uri_string() |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 105 | |
Andrey Andreev | 08f0f8b | 2012-11-09 10:27:43 +0200 | [diff] [blame] | 106 | :returns: string |
| 107 | |
| 108 | Returns the URI segments of any page that contains this function. |
| 109 | For example, if your URL was this:: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 110 | |
| 111 | http://some-site.com/blog/comments/123 |
| 112 | |
Andrey Andreev | 08f0f8b | 2012-11-09 10:27:43 +0200 | [diff] [blame] | 113 | The function would return:: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 114 | |
Melvin Lammerts | 3b1e6e6 | 2013-01-23 22:46:16 +0100 | [diff] [blame] | 115 | blog/comments/123 |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 116 | |
Andrey Andreev | 08f0f8b | 2012-11-09 10:27:43 +0200 | [diff] [blame] | 117 | This function is an alias for ``CI_Config::uri_string()``. For more info, |
| 118 | please see the :doc:`Config Library <../libraries/config>` documentation. |
| 119 | |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 120 | index_page() |
| 121 | ============ |
| 122 | |
Andrey Andreev | 08f0f8b | 2012-11-09 10:27:43 +0200 | [diff] [blame] | 123 | .. php:function:: index_page() |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 124 | |
Andrey Andreev | 08f0f8b | 2012-11-09 10:27:43 +0200 | [diff] [blame] | 125 | :returns: string |
| 126 | |
| 127 | Returns your site **index_page**, as specified in your config file. |
| 128 | Example:: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 129 | |
| 130 | echo index_page(); |
| 131 | |
| 132 | anchor() |
| 133 | ======== |
| 134 | |
Andrey Andreev | 08f0f8b | 2012-11-09 10:27:43 +0200 | [diff] [blame] | 135 | .. php:function:: anchor($uri = '', $title = '', $attributes = '') |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 136 | |
Andrey Andreev | 08f0f8b | 2012-11-09 10:27:43 +0200 | [diff] [blame] | 137 | :param string $uri: URI string |
| 138 | :param string $title: Anchor title |
| 139 | :param mixed $attributes: HTML attributes |
| 140 | :returns: string |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 141 | |
Andrey Andreev | 08f0f8b | 2012-11-09 10:27:43 +0200 | [diff] [blame] | 142 | Creates a standard HTML anchor link based on your local site URL. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 143 | |
| 144 | The first parameter can contain any segments you wish appended to the |
Andrey Andreev | 08f0f8b | 2012-11-09 10:27:43 +0200 | [diff] [blame] | 145 | URL. As with the :php:func:`site_url()` function above, segments can |
| 146 | be a string or an array. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 147 | |
| 148 | .. note:: If you are building links that are internal to your application |
Andrey Andreev | 08f0f8b | 2012-11-09 10:27:43 +0200 | [diff] [blame] | 149 | 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 Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 152 | |
| 153 | The second segment is the text you would like the link to say. If you |
| 154 | leave it blank, the URL will be used. |
| 155 | |
| 156 | The third parameter can contain a list of attributes you would like |
| 157 | added to the link. The attributes can be a simple string or an |
| 158 | associative array. |
| 159 | |
Andrey Andreev | 08f0f8b | 2012-11-09 10:27:43 +0200 | [diff] [blame] | 160 | Here are some examples:: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 161 | |
| 162 | echo anchor('news/local/123', 'My News', 'title="News title"'); |
Andrey Andreev | 08f0f8b | 2012-11-09 10:27:43 +0200 | [diff] [blame] | 163 | // Prints: <a href="http://example.com/index.php/news/local/123" title="News title">My News</a> |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 164 | |
| 165 | echo anchor('news/local/123', 'My News', array('title' => 'The best news!')); |
Andrey Andreev | 08f0f8b | 2012-11-09 10:27:43 +0200 | [diff] [blame] | 166 | // Prints: <a href="http://example.com/index.php/news/local/123" title="The best news!">My News</a> |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 167 | |
Andrey Andreev | 08f0f8b | 2012-11-09 10:27:43 +0200 | [diff] [blame] | 168 | echo anchor('', 'Click here'); |
| 169 | // Prints: <a href="http://example.com">Click Here</a> |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 170 | |
| 171 | anchor_popup() |
| 172 | ============== |
| 173 | |
Andrey Andreev | 08f0f8b | 2012-11-09 10:27:43 +0200 | [diff] [blame] | 174 | .. php:function:: anchor_popup($uri = '', $title = '', $attributes = FALSE) |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 175 | |
Andrey Andreev | 08f0f8b | 2012-11-09 10:27:43 +0200 | [diff] [blame] | 176 | :param string $uri: URI string |
| 177 | :param string $title: Anchor title |
| 178 | :param mixed $attributes: HTML attributes |
| 179 | :returns: string |
| 180 | |
| 181 | Nearly identical to the :php:func:``anchor()`` function except that it |
| 182 | opens the URL in a new window. You can specify JavaScript window |
| 183 | attributes in the third parameter to control how the window is opened. |
| 184 | If the third parameter is not set it will simply open a new window with |
| 185 | your own browser settings. |
| 186 | |
| 187 | Here is an example with attributes:: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 188 | |
Andrey Andreev | 81c3208 | 2012-06-16 21:21:46 +0300 | [diff] [blame] | 189 | $atts = array( |
Andrey Andreev | 08f0f8b | 2012-11-09 10:27:43 +0200 | [diff] [blame] | 190 | 'width' => 800, |
| 191 | 'height' => 600, |
Andrey Andreev | 81c3208 | 2012-06-16 21:21:46 +0300 | [diff] [blame] | 192 | 'scrollbars' => 'yes', |
| 193 | 'status' => 'yes', |
| 194 | 'resizable' => 'yes', |
Andrey Andreev | 08f0f8b | 2012-11-09 10:27:43 +0200 | [diff] [blame] | 195 | 'screenx' => 0, |
| 196 | 'screeny' => 0, |
Andrey Andreev | 81c3208 | 2012-06-16 21:21:46 +0300 | [diff] [blame] | 197 | 'window_name' => '_blank' |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 198 | ); |
| 199 | |
| 200 | echo anchor_popup('news/local/123', 'Click Me!', $atts); |
| 201 | |
Andrey Andreev | 81c3208 | 2012-06-16 21:21:46 +0300 | [diff] [blame] | 202 | .. note:: The above attributes are the function defaults so you only need to |
Derek Jones | ce79be0 | 2012-06-25 23:23:46 -0700 | [diff] [blame] | 203 | 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 Andreev | 08f0f8b | 2012-11-09 10:27:43 +0200 | [diff] [blame] | 205 | third parameter: |
| 206 | | |
| 207 | | echo anchor_popup('news/local/123', 'Click Me!', array()); |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 208 | |
Andrey Andreev | 08f0f8b | 2012-11-09 10:27:43 +0200 | [diff] [blame] | 209 | .. note:: The **window_name** is not really an attribute, but an argument to |
Andrey Andreev | 81c3208 | 2012-06-16 21:21:46 +0300 | [diff] [blame] | 210 | 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 Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 216 | mailto() |
| 217 | ======== |
| 218 | |
Andrey Andreev | 08f0f8b | 2012-11-09 10:27:43 +0200 | [diff] [blame] | 219 | .. php:function:: mailto($email, $title = '', $attributes = '') |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 220 | |
Andrey Andreev | 08f0f8b | 2012-11-09 10:27:43 +0200 | [diff] [blame] | 221 | :param string $email: E-mail address |
| 222 | :param string $title: Anchor title |
| 223 | :param mixed $attributes: HTML attributes |
| 224 | :returns: string |
| 225 | |
| 226 | Creates a standard HTML e-mail link. Usage example:: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 227 | |
| 228 | echo mailto('me@my-site.com', 'Click Here to Contact Me'); |
| 229 | |
Andrey Andreev | 08f0f8b | 2012-11-09 10:27:43 +0200 | [diff] [blame] | 230 | As with the :php:func:`anchor()` tab above, you can set attributes using the |
| 231 | third parameter:: |
InFog | 00b3df4 | 2012-07-29 13:42:50 -0300 | [diff] [blame] | 232 | |
Andrey Andreev | 08f0f8b | 2012-11-09 10:27:43 +0200 | [diff] [blame] | 233 | $attributes = array('title' => 'Mail me'); |
| 234 | echo mailto('me@my-site.com', 'Contact Me', $attributes); |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 235 | |
| 236 | safe_mailto() |
| 237 | ============= |
| 238 | |
Andrey Andreev | 08f0f8b | 2012-11-09 10:27:43 +0200 | [diff] [blame] | 239 | .. 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 | |
| 246 | Identical to the :php:func:`mailto()` function except it writes an obfuscated |
| 247 | version of the *mailto* tag using ordinal numbers written with JavaScript to |
| 248 | help prevent the e-mail address from being harvested by spam bots. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 249 | |
| 250 | auto_link() |
| 251 | =========== |
| 252 | |
Andrey Andreev | 08f0f8b | 2012-11-09 10:27:43 +0200 | [diff] [blame] | 253 | .. php:function:: auto_link($str, $type = 'both', $popup = FALSE) |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 254 | |
Andrey Andreev | 08f0f8b | 2012-11-09 10:27:43 +0200 | [diff] [blame] | 255 | :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 | |
| 260 | Automatically turns URLs and e-mail addresses contained in a string into |
| 261 | links. Example:: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 262 | |
| 263 | $string = auto_link($string); |
| 264 | |
Andrey Andreev | 08f0f8b | 2012-11-09 10:27:43 +0200 | [diff] [blame] | 265 | The second parameter determines whether URLs and e-mails are converted or |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 266 | just one or the other. Default behavior is both if the parameter is not |
Andrey Andreev | 08f0f8b | 2012-11-09 10:27:43 +0200 | [diff] [blame] | 267 | specified. E-mail links are encoded as :php:func:`safe_mailto()` as shown |
| 268 | above. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 269 | |
Andrey Andreev | 08f0f8b | 2012-11-09 10:27:43 +0200 | [diff] [blame] | 270 | Converts only URLs:: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 271 | |
| 272 | $string = auto_link($string, 'url'); |
| 273 | |
Andrey Andreev | 08f0f8b | 2012-11-09 10:27:43 +0200 | [diff] [blame] | 274 | Converts only e-mail addresses:: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 275 | |
| 276 | $string = auto_link($string, 'email'); |
| 277 | |
| 278 | The third parameter determines whether links are shown in a new window. |
Andrey Andreev | 08f0f8b | 2012-11-09 10:27:43 +0200 | [diff] [blame] | 279 | The value can be TRUE or FALSE (boolean):: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 280 | |
| 281 | $string = auto_link($string, 'both', TRUE); |
| 282 | |
| 283 | url_title() |
| 284 | =========== |
| 285 | |
Andrey Andreev | 08f0f8b | 2012-11-09 10:27:43 +0200 | [diff] [blame] | 286 | .. 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 Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 293 | Takes a string as input and creates a human-friendly URL string. This is |
| 294 | useful if, for example, you have a blog in which you'd like to use the |
Andrey Andreev | 08f0f8b | 2012-11-09 10:27:43 +0200 | [diff] [blame] | 295 | title of your entries in the URL. Example:: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 296 | |
| 297 | $title = "What's wrong with CSS?"; |
Andrey Andreev | 08f0f8b | 2012-11-09 10:27:43 +0200 | [diff] [blame] | 298 | $url_title = url_title($title); |
| 299 | // Produces: Whats-wrong-with-CSS |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 300 | |
| 301 | The second parameter determines the word delimiter. By default dashes |
Andrey Andreev | 08f0f8b | 2012-11-09 10:27:43 +0200 | [diff] [blame] | 302 | are used. Preferred options are: **-** (dash) or **_** (underscore) |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 303 | |
Andrey Andreev | 08f0f8b | 2012-11-09 10:27:43 +0200 | [diff] [blame] | 304 | Example:: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 305 | |
| 306 | $title = "What's wrong with CSS?"; |
Andrey Andreev | 08f0f8b | 2012-11-09 10:27:43 +0200 | [diff] [blame] | 307 | $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 Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 312 | |
| 313 | The third parameter determines whether or not lowercase characters are |
Andrey Andreev | 08f0f8b | 2012-11-09 10:27:43 +0200 | [diff] [blame] | 314 | forced. By default they are not. Options are boolean TRUE/FALSE. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 315 | |
Andrey Andreev | 08f0f8b | 2012-11-09 10:27:43 +0200 | [diff] [blame] | 316 | Example:: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 317 | |
| 318 | $title = "What's wrong with CSS?"; |
Andrey Andreev | 08f0f8b | 2012-11-09 10:27:43 +0200 | [diff] [blame] | 319 | $url_title = url_title($title, 'underscore', TRUE); |
| 320 | // Produces: whats_wrong_with_css |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 321 | |
| 322 | prep_url() |
| 323 | ---------- |
| 324 | |
Andrey Andreev | 08f0f8b | 2012-11-09 10:27:43 +0200 | [diff] [blame] | 325 | .. php:function:: prep_url($str = '') |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 326 | |
Andrey Andreev | 08f0f8b | 2012-11-09 10:27:43 +0200 | [diff] [blame] | 327 | :param string $str: URL string |
| 328 | :returns: string |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 329 | |
Andrey Andreev | 08f0f8b | 2012-11-09 10:27:43 +0200 | [diff] [blame] | 330 | This function will add http:// in the event that a protocol prefix |
| 331 | is missing from a URL. |
| 332 | |
| 333 | Pass the URL string to the function like this:: |
| 334 | |
| 335 | $url = prep_url('example.com'); |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 336 | |
| 337 | redirect() |
| 338 | ========== |
| 339 | |
Andrey Andreev | 08f0f8b | 2012-11-09 10:27:43 +0200 | [diff] [blame] | 340 | .. 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 Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 347 | Does a "header redirect" to the URI specified. If you specify the full |
Brandon Jones | 50e5dbb | 2011-11-07 15:51:05 -0500 | [diff] [blame] | 348 | site URL that link will be built, but for local links simply providing |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 349 | the URI segments to the controller you want to direct to will create the |
| 350 | link. The function will build the URL based on your config file values. |
| 351 | |
Brandon Jones | 50e5dbb | 2011-11-07 15:51:05 -0500 | [diff] [blame] | 352 | The optional second parameter allows you to force a particular redirection |
Andrey Andreev | 08f0f8b | 2012-11-09 10:27:43 +0200 | [diff] [blame] | 353 | method. The available methods are **auto**, **location** and **refresh**, |
| 354 | with location being faster but less reliable on IIS servers. |
| 355 | The default is **auto**, which will attempt to intelligently choose the |
| 356 | method based on the server environment. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 357 | |
Brandon Jones | 50e5dbb | 2011-11-07 15:51:05 -0500 | [diff] [blame] | 358 | The optional third parameter allows you to send a specific HTTP Response |
| 359 | Code - this could be used for example to create 301 redirects for search |
| 360 | engine purposes. The default Response Code is 302. The third parameter is |
Andrey Andreev | 08f0f8b | 2012-11-09 10:27:43 +0200 | [diff] [blame] | 361 | *only* available with **location** redirects, and not *refresh*. Examples:: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 362 | |
| 363 | if ($logged_in == FALSE) |
| 364 | { |
Brandon Jones | 50e5dbb | 2011-11-07 15:51:05 -0500 | [diff] [blame] | 365 | redirect('/login/form/'); |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 366 | } |
| 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 Andreev | 08f0f8b | 2012-11-09 10:27:43 +0200 | [diff] [blame] | 375 | `Output Library </libraries/output>` ``set_header()`` method. |
vlakoff | 530b946 | 2012-09-17 14:18:07 +0200 | [diff] [blame] | 376 | |
Andrey Andreev | 08f0f8b | 2012-11-09 10:27:43 +0200 | [diff] [blame] | 377 | .. note:: To IIS users: if you hide the `Server` HTTP header, the *auto* |
vlakoff | 530b946 | 2012-09-17 14:18:07 +0200 | [diff] [blame] | 378 | method won't detect IIS, in that case it is advised you explicitly |
Andrey Andreev | 08f0f8b | 2012-11-09 10:27:43 +0200 | [diff] [blame] | 379 | 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 Andreev | 1a00149 | 2013-01-24 11:32:29 +0200 | [diff] [blame] | 385 | .. important:: This function will terminate script execution. |