Returns a table with the seconds and nanoseconds at the time it was called.
local before = clock();
do_stuff();
local after = clock();
local difference = ((after .seconds*1000000000)+after .nanoseconds) -
((before .seconds*1000000000)+before .nanoseconds);
echo("do_stuff() took ", difference/1000000, " milliseconds");
Hint: you can use the function compileTime() as you would clock() to get the precise time the mod_plua handler started handling the lua file
dbtype, parameters):(See more information on the [Database connectivity] page.)
Tries to open up a connection to a database of type dbtype with the parameters set in parameters. This function uses APR_DBD to do the actual handling, so you should consult this for a list of supported databases and parameters. The following databases (or handlers) have been successfully tested with mod_pLua:
If an error occurs, open() returns nil as the first argument and an error string explaining the situation as the second argument (though, no error will be returned if "mod_dbd" is the database driver).
If successful, it returns a table with functions for handling the database:
Returns true if an active database handle exists, false otherwise.
Use this to check if your database connection still exists or if it was closed or GC'ed.
Closes the database connection and cleans up.
value):Escapes the text specified in value for use in SQL statements. The escape routine used is the one native to each database type.
statement):Queries the database using statement and returns a table containing all the rows returned.
Each row contains a table of the individual columns fetched as index 1,2,3...
If query() fails to execute, it will return nil as the first argument and the error message as the second.
statement):Runs statement in the opened database and returns the number of rows affected.
If run() fails to execute, it will return nil as the first argument and the error message as the second.
local db = dbopen("sqlite3", "mydatabase.sqlite");
if (db) then
db:run("INSERT INTO `myTable` VALUES (1,2,3,4,5)");
local results = db:query("SELECT * FROM `myTable` WHERE 1");
echo("We got " .. #results .. " results!");
db:close();
end
You can use the garbage collector to close unwanted handled as well, although the preferred way is to use db:close. The example below demonstrates how to create a persistent database connection that reopens if it has been closed:
db = db or false;
if not db or not db:active() then
db, err = dbopen("sqlite3", "mydatabase.sqlite");
end
...):Same as print(...); Prints out variables to the HTTP stream.
echo("Cookies ", "are ", "Awesome", "!", 1+2+3+4+5);
Exits the current script, halting any further execution.
This can, for example, be used to stop a script if conditions are not met
if (user ~= "JohnDoe") then exit() end
print("This should not show unless you are JohnDoe");
filename):Returns true if the file (or folder) exists, false otherwise.
if (file.exists("/path/to/file")) then
echo("The file exists!");
end
filename, newfilename):Renames (moves) the file filename to newfilename. Returns true if successful, false otherwise.
if (file.exists("/path/to/file")) then
file.rename("/path/to/file", "/new/path/to/new/file");
end
filename):Deletes the file filename, returns true if successful, false otherwise.
if (file.exists("/path/to/file")) then
file.unlink("/path/to/file");
end
filename):Sends the file filename to the client, using sendfile if possible. On success, returns the number of bytes that was sent, otherwise returns false.
Using file.send speeds up the transaction significantly by not having to read the file into a buffer before sending it to the client browser.
if (file.exists("/path/to/file")) then
local bytesSent = file.send("/path/to/file"); -- #send file directly to browser
end
filename):Returns a table containing the statistics of filename:
local stat = file.stat("/path/to/file");
if (stat) then
echo("File was modified at ", stat.modified);
end
Returns a table of all the environment variables available.
local env = getEnv();
echo("You are asking for ", env["Host"]);
GetEnv also returns information about the mod_plua system, such as its configuration and version.
[filename]):Reads the request body. If filename is specified, the request body is written to filename instead (useful where memory consumption is a concern), and the total size of the request body is returned.
If no filename is specified, it returns the entire request body as well as the size of the body.
getRequestBody() works only with PUT and POST requests.
local bytesWritten = getRequestBody("output.txt");
if (bytesWritten) then
print("You sent a file that was ", bytesWritten, " bytes!");
end
key, value):Sets the output HTTP header key to value.
header("Set-Cookie", "myCookie=awesome"); --# Set a cookie
header("Location", "/somewhere_else"); --# redirect to /somewhere_else
errorCode):Tells Apache to generate and display an error message that corresponds to the HTTP status code errorCode
httpError(404); --# Will display a "File not found" message to the browser.
If you only wish to set a return code, but not display automatically generated content (for example for basic authorization), you should use setReturnCode() instead.
filename):Includes another mod_plua script in the code. This is similar to the PHP include() function, as it preprocesses the included file as it would a regular plua file.
Note: This is NOT the same as require() or dofile().
echo("Before header include");
include("header.plua");
echo("After header include");
Returns a table containing the key/value pairs from sent via GET
Note: If a key has multiple values, it will be returned as a table, so make sure to check for it, specifically when using \<select> form fields.
local params = parseGet();
if (params) then
echo("Hello, " .. (params["name"] or "unknown"));
end
Returns a table containing the key/value pairs from sent via POST.
local params = parsePost();
if (params) then
echo("Hello, " .. (params["name"] or "unknown"));
end
Any form data with multiple values defined will cause parseGet and parsePost to return this key/value pair as a table with value[1] as the first value, value[2] as the second and so on.
In case of multipart/form-data posts with files attached, each uploaded file will be returned as a table with value[1] being the filename and value[2] being the contents of the file. In cases where no filename is specified, value[1] will contain the file contents.
value):Sets the content type of the output to value.
setContentType("text/plain");
echo("This is plain text, no <html> will be parsed");
N):Sets the HTTP return code to N.
setReturnCode(401); --# Server now will respond "Authorization required"
note: If you wish to invoke server generated replies, such as a 404 message, you should use httpError() instead.
text):Encodes text using Base64 encoding
local txt = string.encode64("Testing");
print(txt); --# Prints 'VGVzdGluZw=='
text):Decodes text using Base64 encoding
local txt = string.decode64("VGVzdGluZw==");
print(txt); --# Prints 'Testing'
text, delimiter):Chops up a string into bits as marked by delimiter. Returns a table with each segment as an element. The value of delimiter can be that of a string or a character. If an empty string is given as delimiter, the value of text will be split into an array with each letter of text as an element.
local text = "Hello, world!"
local array = string.explode(text, ", "); --# Chop up into "Hello" and "world!"
print(array[2]); --# Print "world!"
text):Creates an MD5 digest based on text
local txt = string.md5("The quick brown fox jumps over the lazy dog");
print(txt); --# Prints '9e107d9d372bb6826bd81d3542a419d6'
text):Creates a SHA256 digest based on text
local txt = string.SHA256("The quick brown fox jumps over the lazy dog");
print(txt); --# Prints 'd7a8fbb307d7809469ca9abcb0082e4f8d5651e46d3cdb762d02d0bf37c9e592'