blob: 65ca026a1d7c3d60797af5be260241a8911e45d6 [file] [log] [blame]
Derek Jones8ede1a22011-10-05 13:34:52 -05001################
2Common Functions
3################
4
5CodeIgniter uses a few functions for its operation that are globally
6defined, and are available to you at any point. These do not require
7loading any libraries or helpers.
8
Andrey Andreev9f387e72014-01-07 11:50:34 +02009.. contents::
10 :local:
11
12.. raw:: html
13
14 <div class="custom-index container"></div>
Derek Jones8ede1a22011-10-05 13:34:52 -050015
Andrey Andreevea801ab2014-01-20 15:03:43 +020016.. function:: is_php([$version = '5.3.0'])
Derek Jones8ede1a22011-10-05 13:34:52 -050017
Andrey Andreev1bc30262012-11-09 11:30:51 +020018 :param string $version: Version number
19 :returns: bool
Derek Jones8ede1a22011-10-05 13:34:52 -050020
Andrey Andreev9f387e72014-01-07 11:50:34 +020021 Determines of the PHP version being used is greater than the
22 supplied version number.
Andrey Andreev1bc30262012-11-09 11:30:51 +020023
Andrey Andreev9f387e72014-01-07 11:50:34 +020024 Example::
Andrey Andreev1bc30262012-11-09 11:30:51 +020025
Andrey Andreev9f387e72014-01-07 11:50:34 +020026 if (is_php('5.3'))
27 {
28 $str = quoted_printable_encode($str);
29 }
Derek Jones8ede1a22011-10-05 13:34:52 -050030
Andrey Andreev9f387e72014-01-07 11:50:34 +020031 Returns boolean TRUE if the installed version of PHP is equal to or
32 greater than the supplied version number. Returns FALSE if the installed
33 version of PHP is lower than the supplied version number.
Derek Jones8ede1a22011-10-05 13:34:52 -050034
Andrey Andreev9f387e72014-01-07 11:50:34 +020035 .. function:: is_really_writable($file)
Derek Jones8ede1a22011-10-05 13:34:52 -050036
Andrey Andreev9f387e72014-01-07 11:50:34 +020037 :param string $file: File path
38 :returns: bool
Andrey Andreev1bc30262012-11-09 11:30:51 +020039
Andrey Andreev9f387e72014-01-07 11:50:34 +020040 ``is_writable()`` returns TRUE on Windows servers when you really can't
41 write to the file as the OS reports to PHP as FALSE only if the
42 read-only attribute is marked.
Andrey Andreev1bc30262012-11-09 11:30:51 +020043
Andrey Andreev9f387e72014-01-07 11:50:34 +020044 This function determines if a file is actually writable by attempting
45 to write to it first. Generally only recommended on platforms where
46 this information may be unreliable.
Derek Jones8ede1a22011-10-05 13:34:52 -050047
Andrey Andreev9f387e72014-01-07 11:50:34 +020048 Example::
Andrey Andreev1bc30262012-11-09 11:30:51 +020049
Andrey Andreev9f387e72014-01-07 11:50:34 +020050 if (is_really_writable('file.txt'))
51 {
52 echo "I could write to this if I wanted to";
53 }
54 else
55 {
56 echo "File is not writable";
57 }
Andrey Andreev1bc30262012-11-09 11:30:51 +020058
Derek Jonesb8c283a2013-07-19 16:02:53 -070059.. function:: config_item($key)
Andrey Andreev1bc30262012-11-09 11:30:51 +020060
61 :param string $key: Config item key
62 :returns: mixed
Derek Jones8ede1a22011-10-05 13:34:52 -050063
Andrey Andreev9f387e72014-01-07 11:50:34 +020064 The :doc:`Config Library <../libraries/config>` is the preferred way of
65 accessing configuration information, however ``config_item()`` can be used
66 to retrieve single keys. See :doc:`Config Library <../libraries/config>`
67 documentation for more information.
Derek Jones8ede1a22011-10-05 13:34:52 -050068
Andrey Andreevea801ab2014-01-20 15:03:43 +020069.. :noindex: function:: show_error($message, $status_code[, $heading = 'An Error Was Encountered'])
Derek Jones8ede1a22011-10-05 13:34:52 -050070
Andrey Andreev1bc30262012-11-09 11:30:51 +020071 :param mixed $message: Error message
72 :param int $status_code: HTTP Response status code
73 :param string $heading: Error page heading
74 :returns: void
75
Andrey Andreev9f387e72014-01-07 11:50:34 +020076 This function calls ``CI_Exception::show_error()``. For more info,
77 please see the :doc:`Error Handling <errors>` documentation.
Andrey Andreev1bc30262012-11-09 11:30:51 +020078
Andrey Andreevea801ab2014-01-20 15:03:43 +020079.. :noindex: function:: show_404([$page = ''[, $log_error = TRUE]])
Andrey Andreev1bc30262012-11-09 11:30:51 +020080
81 :param string $page: URI string
82 :param bool $log_error: Whether to log the error
83 :returns: void
84
Andrey Andreev9f387e72014-01-07 11:50:34 +020085 This function calls ``CI_Exception::show_404()``. For more info,
86 please see the :doc:`Error Handling <errors>` documentation.
Andrey Andreev1bc30262012-11-09 11:30:51 +020087
Andrey Andreevea801ab2014-01-20 15:03:43 +020088.. :noindex: function:: log_message($level, $message)
Andrey Andreev1bc30262012-11-09 11:30:51 +020089
vlakoffd0c30ab2013-05-07 07:49:23 +020090 :param string $level: Log level: 'error', 'debug' or 'info'
Andrey Andreev1bc30262012-11-09 11:30:51 +020091 :param string $message: Message to log
Andrey Andreev1bc30262012-11-09 11:30:51 +020092 :returns: void
93
Andrey Andreev9f387e72014-01-07 11:50:34 +020094 This function is an alias for ``CI_Log::write_log()``. For more info,
95 please see the :doc:`Error Handling <errors>` documentation.
Derek Jones8ede1a22011-10-05 13:34:52 -050096
Andrey Andreevea801ab2014-01-20 15:03:43 +020097.. function:: set_status_header($code[, $text = ''])
Andrey Andreev1bc30262012-11-09 11:30:51 +020098
99 :param int $code: HTTP Reponse status code
100 :param string $text: A custom message to set with the status code
101 :returns: void
102
Andrey Andreev9f387e72014-01-07 11:50:34 +0200103 Permits you to manually set a server status header. Example::
Derek Jones8ede1a22011-10-05 13:34:52 -0500104
Andrey Andreev9f387e72014-01-07 11:50:34 +0200105 set_status_header(401);
106 // Sets the header as: Unauthorized
Derek Jones8ede1a22011-10-05 13:34:52 -0500107
Andrey Andreev9f387e72014-01-07 11:50:34 +0200108 `See here <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html>`_ for
109 a full list of headers.
Derek Jones8ede1a22011-10-05 13:34:52 -0500110
Andrey Andreevea801ab2014-01-20 15:03:43 +0200111.. function:: remove_invisible_characters($str[, $url_encoded = TRUE])
Andrey Andreev1bc30262012-11-09 11:30:51 +0200112
113 :param string $str: Input string
114 :param bool $url_encoded: Whether to remove URL-encoded characters as well
115 :returns: string
116
Andrey Andreev9f387e72014-01-07 11:50:34 +0200117 This function prevents inserting NULL characters between ASCII
118 characters, like Java\\0script.
Derek Jones8ede1a22011-10-05 13:34:52 -0500119
Andrey Andreev9f387e72014-01-07 11:50:34 +0200120 Example::
Derek Jones8ede1a22011-10-05 13:34:52 -0500121
Andrey Andreev9f387e72014-01-07 11:50:34 +0200122 remove_invisible_characters('Java\\0script');
123 // Returns: 'Javascript'
Andrey Andreev1bc30262012-11-09 11:30:51 +0200124
Derek Jonesb8c283a2013-07-19 16:02:53 -0700125.. function:: html_escape($var)
Andrey Andreev1bc30262012-11-09 11:30:51 +0200126
Andrey Andreev9f387e72014-01-07 11:50:34 +0200127 :param mixed $var: Variable to escape (string or array)
Andrey Andreev1bc30262012-11-09 11:30:51 +0200128 :returns: mixed
129
Andrey Andreev9f387e72014-01-07 11:50:34 +0200130 This function acts as an alias for PHP's native ``htmlspecialchars()``
131 function, with the advantage of being able to accept an array of strings.
Andrey Andreev1bc30262012-11-09 11:30:51 +0200132
Andrey Andreev9f387e72014-01-07 11:50:34 +0200133 It is useful in preventing Cross Site Scripting (XSS).
Andrey Andreev6ef498b2012-06-05 22:01:58 +0300134
Derek Jonesb8c283a2013-07-19 16:02:53 -0700135.. function:: get_mimes()
Andrey Andreev1bc30262012-11-09 11:30:51 +0200136
137 :returns: array
138
Andrey Andreev9f387e72014-01-07 11:50:34 +0200139 This function returns a *reference* to the MIMEs array from
140 *application/config/mimes.php*.
Andrey Andreev3fb02672012-10-22 16:48:01 +0300141
Derek Jonesb8c283a2013-07-19 16:02:53 -0700142.. function:: is_https()
Andrey Andreev1bc30262012-11-09 11:30:51 +0200143
144 :returns: bool
145
Andrey Andreev9f387e72014-01-07 11:50:34 +0200146 Returns TRUE if a secure (HTTPS) connection is used and FALSE
147 in any other case (including non-HTTP requests).
Andrey Andreeve9d2dc82012-11-07 14:23:29 +0200148
Andrey Andreevea801ab2014-01-20 15:03:43 +0200149.. function:: is_cli()
Andrey Andreevf964b162013-11-12 17:04:55 +0200150
151 :returns: bool
152
Andrey Andreevea801ab2014-01-20 15:03:43 +0200153 Returns TRUE if the application is run through the command line
154 and FALSE if not.
Andrey Andreevf964b162013-11-12 17:04:55 +0200155
Andrey Andreevea801ab2014-01-20 15:03:43 +0200156 .. note:: This function checks both if the ``PHP_SAPI`` value is 'cli'
157 or if the ``STDIN`` constant is defined.
Andrey Andreeve9d2dc82012-11-07 14:23:29 +0200158
Derek Jonesb8c283a2013-07-19 16:02:53 -0700159.. function:: function_usable($function_name)
Andrey Andreev1bc30262012-11-09 11:30:51 +0200160
161 :param string $function_name: Function name
162 :returns: bool
163
Andrey Andreev9f387e72014-01-07 11:50:34 +0200164 Returns TRUE if a function exists and is usable, FALSE otherwise.
Andrey Andreeve9d2dc82012-11-07 14:23:29 +0200165
Andrey Andreev9f387e72014-01-07 11:50:34 +0200166 This function runs a ``function_exists()`` check and if the
167 `Suhosin extension <http://www.hardened-php.net/suhosin/>` is loaded,
168 checks if it doesn't disable the function being checked.
Andrey Andreeve9d2dc82012-11-07 14:23:29 +0200169
Andrey Andreev9f387e72014-01-07 11:50:34 +0200170 It is useful if you want to check for the availability of functions
171 such as ``eval()`` and ``exec()``, which are dangerous and might be
172 disabled on servers with highly restrictive security policies.