URL Helper

The URL Helper file contains functions that assist in working with URLs.

Loading this Helper

This helper is automatically loaded by the framework on every request.

Available Functions

The following functions are available:

  • siteurl([$uri = ''[, $protocol = NULL[, $altConfig = NULL_]]])

Parameters:

  • $uri (mixed) – URI string or array of URI segments
  • $protocol (string) – Protocol, e.g. ‘http’ or ‘https’
  • $altConfig (\Config\App) – Alternate configuration to useReturns:Site URLReturn type:string

Returns your site URL, as specified in your config file. The index.phpfile (or whatever you have set as your site indexPage in your configfile) will be added to the URL, as will any URI segments you pass to thefunction.

You are encouraged to use this function any time you need to generate alocal URL so that your pages become more portable in the event your URLchanges.

Segments can be optionally passed to the function as a string or anarray. Here is a string example:

  1. echo site_url('news/local/123');

The above example would return something like:http://example.com/index.php/news/local/123

Here is an example of segments passed as an array:

  1. $segments = ['news', 'local', '123'];
  2. echo site_url($segments);

You may find the alternate configuration useful if generating URLs for adifferent site than yours, which contains different configuration preferences.We use this for unit testing the framework itself.

  • baseurl([$uri = ''[, $protocol = NULL_]])

Parameters:

  • $uri (mixed) – URI string or array of URI segments
  • $protocol (string) – Protocol, e.g. ‘http’ or ‘https’Returns:Base URLReturn type:string

Returns your site base URL, as specified in your config file. Example:

  1. echo base_url();

This function returns the same thing as site_url(), withoutthe indexPage being appended.

Also like site_url(), you can supply segments as a string oran array. Here is a string example:

  1. echo base_url("blog/post/123");

The above example would return something like:http://example.com/blog/post/123

This is useful because unlike site_url(), you can supply astring to a file, such as an image or stylesheet. For example:

  1. echo base_url("images/icons/edit.png");

This would give you something like:http://example.com/images/icons/edit.png

  • currenturl([$returnObject = false_])

Parameters:

  • $returnObject (boolean) – True if you would like a URI instance returned, instead of a string.Returns:The current URLReturn type:string|URI

Returns the full URL (including segments) of the page being currentlyviewed.

Note

Calling this function is the same as doing this::base_url(uri_string());

  • previousurl([$returnObject = false_])

Parameters:

  • $returnObject (boolean) – True if you would like a URI instance returned instead of a string.Returns:The URL the user was previously onReturn type:string|URI

Returns the full URL (including segments) of the page the user was previously on.

Due to security issues of blindly trusting the HTTP_REFERER system variable, CodeIgniter willstore previously visited pages in the session if it’s available. This ensures that we alwaysuse a known and trusted source. If the session hasn’t been loaded, or is otherwise unavailable,then a sanitized version of HTTP_REFERER will be used.

  • uri_string()

Returns:An URI stringReturn type:string

Returns the path part of your current URL.For example, if your URL was this:

  1. http://some-site.com/blog/comments/123

The function would return:

  1. blog/comments/123
  • indexpage([$altConfig = NULL_])

Parameters:

  • $altConfig (ConfigApp) – Alternate configuration to useReturns:‘index_page’ valueReturn type:mixed

Returns your site indexPage, as specified in your config file.Example:

  1. echo index_page();

As with site_url(), you may specify an alternate configuration.You may find the alternate configuration useful if generating URLs for adifferent site than yours, which contains different configuration preferences.We use this for unit testing the framework itself.

  • anchor([$uri = ''[, $title = ''[, $attributes = ''[, $altConfig = NULL]]]])

Parameters:

  • $uri (mixed) – URI string or array of URI segments
  • $title (string) – Anchor title
  • $attributes (mixed) – HTML attributes
  • $altConfig (ConfigApp) – Alternate configuration to useReturns:HTML hyperlink (anchor tag)Return type:string

Creates a standard HTML anchor link based on your local site URL.

The first parameter can contain any segments you wish appended to theURL. As with the site_url() function above, segments canbe a string or an array.

Note

If you are building links that are internal to your applicationdo not include the base URL (http://…). This will be addedautomatically from the information specified in your config file.Include only the URI segments you wish appended to the URL.

The second segment is the text you would like the link to say. If youleave it blank, the URL will be used.

The third parameter can contain a list of attributes you would likeadded to the link. The attributes can be a simple string or anassociative array.

Here are some examples:

  1. echo anchor('news/local/123', 'My News', 'title="News title"');
  2. // Prints: <a href="http://example.com/index.php/news/local/123" title="News title">My News</a>
  3.  
  4. echo anchor('news/local/123', 'My News', ['title' => 'The best news!']);
  5. // Prints: <a href="http://example.com/index.php/news/local/123" title="The best news!">My News</a>
  6.  
  7. echo anchor('', 'Click here');
  8. // Prints: <a href="http://example.com/index.php">Click here</a>

As above, you may specify an alternate configuration.You may find the alternate configuration useful if generating links for adifferent site than yours, which contains different configuration preferences.We use this for unit testing the framework itself.

Note

Attributes passed into the anchor function are automatically escaped to protected against XSS attacks.

  • anchorpopup([$uri = ''[, $title = ''[, $attributes = FALSE[, $altConfig = NULL_]]]])

Parameters:

  • $uri (string) – URI string
  • $title (string) – Anchor title
  • $attributes (mixed) – HTML attributes
  • $altConfig (ConfigApp) – Alternate configuration to useReturns:Pop-up hyperlinkReturn type:string

Nearly identical to the anchor() function except that itopens the URL in a new window. You can specify JavaScript windowattributes in the third parameter to control how the window is opened.If the third parameter is not set it will simply open a new window withyour own browser settings.

Here is an example with attributes:

  1. $atts = [
  2. 'width' => 800,
  3. 'height' => 600,
  4. 'scrollbars' => 'yes',
  5. 'status' => 'yes',
  6. 'resizable' => 'yes',
  7. 'screenx' => 0,
  8. 'screeny' => 0,
  9. 'window_name' => '_blank'
  10. ];
  11.  
  12. echo anchor_popup('news/local/123', 'Click Me!', $atts);

Note

The above attributes are the function defaults so you only need toset the ones that are different from what you need. If you want thefunction to use all of its defaults simply pass an empty array in thethird parameter:

  1. echo anchor_popup('news/local/123', 'Click Me!', []);

Note

The window_name is not really an attribute, but an argument tothe JavaScript window.open()method, which accepts either a window name or a window target.

Note

Any other attribute than the listed above will be parsed as anHTML attribute to the anchor tag.

As above, you may specify an alternate configuration.You may find the alternate configuration useful if generating links for adifferent site than yours, which contains different configuration preferences.We use this for unit testing the framework itself.

Note

Attributes passed into the anchor_popup function are automatically escaped to protected against XSS attacks.

  • mailto($email[, $title = ''[, $attributes = '']])

Parameters:

  • $email (string) – E-mail address
  • $title (string) – Anchor title
  • $attributes (mixed) – HTML attributesReturns:A “mail to” hyperlinkReturn type:string

Creates a standard HTML e-mail link. Usage example:

  1. echo mailto('me@my-site.com', 'Click Here to Contact Me');

As with the anchor() tab above, you can set attributes using thethird parameter:

  1. $attributes = ['title' => 'Mail me'];
  2. echo mailto('me@my-site.com', 'Contact Me', $attributes);

Note

Attributes passed into the mailto function are automatically escaped to protected against XSS attacks.

  • safemailto($email[, $title = ''[, $attributes = ''_]])

Parameters:

  • $email (string) – E-mail address
  • $title (string) – Anchor title
  • $attributes (mixed) – HTML attributesReturns:A spam-safe “mail to” hyperlinkReturn type:string

Identical to the mailto() function except it writes an obfuscatedversion of the mailto tag using ordinal numbers written with JavaScript tohelp prevent the e-mail address from being harvested by spam bots.

  • autolink($str[, $type = 'both'[, $popup = FALSE_]])

Parameters:

  • $str (string) – Input string
  • $type (string) – Link type (‘email’, ‘url’ or ‘both’)
  • $popup (bool) – Whether to create popup linksReturns:Linkified stringReturn type:string

Automatically turns URLs and e-mail addresses contained in a string intolinks. Example:

  1. $string = auto_link($string);

The second parameter determines whether URLs and e-mails are converted orjust one or the other. The default behavior is both if the parameter is notspecified. E-mail links are encoded as safe_mailto() as shownabove.

Converts only URLs:

  1. $string = auto_link($string, 'url');

Converts only e-mail addresses:

  1. $string = auto_link($string, 'email');

The third parameter determines whether links are shown in a new window.The value can be TRUE or FALSE (boolean):

  1. $string = auto_link($string, 'both', TRUE);

Note

The only URLs recognized are those that start with “www.” or with “://”.

  • urltitle($str[, $separator = '-'[, $lowercase = FALSE_]])

Parameters:

  • $str (string) – Input string
  • $separator (string) – Word separator
  • $lowercase (bool) – Whether to transform the output string to lower-caseReturns:URL-formatted stringReturn type:string

Takes a string as input and creates a human-friendly URL string. This isuseful if, for example, you have a blog in which you’d like to use thetitle of your entries in the URL. Example:

  1. $title = "What's wrong with CSS?";
  2. $url_title = url_title($title);
  3. // Produces: Whats-wrong-with-CSS

The second parameter determines the word delimiter. By default dashesare used. Preferred options are: - (dash) or _ (underscore).

Example:

  1. $title = "What's wrong with CSS?";
  2. $url_title = url_title($title, 'underscore');
  3. // Produces: Whats_wrong_with_CSS

The third parameter determines whether or not lowercase characters areforced. By default they are not. Options are boolean TRUE/FALSE.

Example:

  1. $title = "What's wrong with CSS?";
  2. $url_title = url_title($title, 'underscore', TRUE);
  3. // Produces: whats_wrong_with_css
  • prepurl($str = ''_)

Parameters:

  • $str (string) – URL stringReturns:Protocol-prefixed URL stringReturn type:string

This function will add http:// in the event that a protocol prefixis missing from a URL.

Pass the URL string to the function like this:

  1. $url = prep_url('example.com');