Categories
Code Snippets PHP

How to get current directory, filename and code line number in PHP

How to Get the Current Directory, Filename, and Code Line Number in PHP

In PHP, there are several built-in functions that can be used to obtain information about the current script’s directory, filename, and code line number.

Getting the Current Directory

To get the absolute path of the directory containing the current script, you can use the dirname(__FILE__) function. For example:

$current_directory = dirname(__FILE__);

This will set the $current_directory variable to the absolute path of the directory containing the current script.

Getting the Current Filename

To get the name of the current script file, you can use the basename(__FILE__) function. For example:

$current_filename = basename(__FILE__);

This will set the $current_filename variable to the name of the current script file.

Getting the Current Code Line Number

To get the current code line number, you can use the __LINE__ magic constant. For example:

$current_line_number = __LINE__;

This will set the $current_line_number variable to the current code line number.

By using these built-in functions and magic constants, you can easily obtain information about the current script’s directory, filename, and code line number in PHP.

Categories
Code Snippets PHP

Loop Through PHP Request Values – GET and POST

Loop through PHP Request Values

In order to Loop through PHP Request Values – GET and POST without knowing what they all might be, you can simply use the following code:

	
foreach ($_REQUEST as $key => $value)  
{  
$value = mysql_real_escape_string( $value );  
$value = addslashes($value);  
$value = strip_tags($value);  
if($value != ""){$requestnumber ++;}  
echo $key. ' - '.$value.'
'; }

This is useful when you have an undetermined/unknown set of POST or GET values that you would like to loop through.

Categories
Code Snippets

Mobile Redirection With Full Site Option

It’s handy for people who don’t know that you have a mobile version of your site, or know what the URL is (is it sitename.com/mobile? Is it mobile.sitename.com? Is it something else?) On the other hand people often want to view the full version of a site on their mobile device. You can achieve this quite simply.

This code goes at the top of your header include file, or at least at the top of each page you wish to redirect. It must go before any page rendering happens, or you will get the dreaded “headers have already been sent” error.



In the header of the mobile site add some code to reset the override cookie to allow the mobile site to be seen. The assumption is that you view the mobile site until you tell the application otherwise:



All that is left to do is to give the manual option for switching between the two versions, in the form of links.

From mobile to full (includes variable to set view mobile to false):

<a href="http://yoursite.com/index.php?mv=0">View Full Website</a>

And the option to return to the mobile version, where the cookie is automatically changed just by visiting:

<a href="http://yoursite.com/mobile/">View Mobile Website</a>

Categories
Code Snippets

Encode Mysql Results to JSON

The title says it all:


mysql_set_charset('utf8', $database_connection); // charset
$sqlStr = "SELECT * FROM yourtable;"; //provide the query
$result = mysql_query ($sqlStr, $database_connection) or die ($sqlStr); // get the results
$rows = array(); // create the array
if (mysql_num_rows($result)) {
while ($row = mysql_fetch_assoc($result)) { // populate array with results
$rows[] = $row;
}
}
echo json_encode($rows); // encode all in json
mysql_close($database_connection); // close the database connection