Tuesday, December 18, 2007

PHP Code View Highlighter

This uses php's highlight_string() function to highlight your php code.

<title>Color Coder</title>
<?php
if( isset( $_POST['content'] ) )
{
$php_code = highlight_string( stripslashes( $_POST['content'] ), true );
?>
<div style="background:#FFFFCC; border:2px solid #999999; width:550px;">
<!-- Start Code -->
<?php echo $php_code . "\n"; ?>
<!-- End Code -->
</div>
<br />
<?php
}
?>
<form method="post" action="">
<textarea name="content" rows="15" cols="45"><?php echo stripslashes( $_POST['content'] ); ?></textarea>
<br />
<input type="submit" value="Color Me!" />
</form>
Just type in your <strong>PHP</strong> code that you would like to be color coded

Friday, December 14, 2007

Use other extension for PHP scripts

To be able to use php scripts on a page with an extension other than default .php, you need a server which supports creating/editing .htaccess files.

To add an extension to be parsed for php, create or edit a file called .htaccess (with the dot first) containing the following line:
AddType application/x-httpd-php .html .foo .htm
This tells the server to check for php code and execute it in files with extensions .html, .foo or .htm. So upload that .htaccess file in the same folder as your .html files with PHP code inside them (using PHP tags: <?php ... ?>) then when you view them in the browser, the PHP should be dealt with as you’d expect from a PHP page.

Another way is by using mod rewrite (here is an excellent tutorial about mod rewrite)

Find the Current URL

The full URL to a web page comes in four parts: The protocol prefix, the domain name, the path to the file and the filename, and the query string. For example, take the URL http://www.example.com/example/page.php?name=Bob. The three parts of this are:
  1. The domain name: www.example.com
  2. The path to the page: /example/page.php
  3. The query string: name=Bob
Here we will show do you how to find current URL with your own PHP scripts...

All of the information we need is stored in the $_SERVER array, which is accessible from anywhere in your PHP script (and as such is called a superglobal variable), it works like a normal array and the keys we wish to retrieve the values of are 'HTTPS', 'HTTP_HOST', 'SCRIPT_NAME' and 'QUERY_STRING' for the four different parts of the url. Alternatively, if we don't need to have the path to the page and the query string seperate, we can use 'REQUEST_URI'.

The following code should let you find it:
//find out the protocol
$protocol = ($_SERVER['HTTPS'] ? 'https://' : 'http://');
//find out the domain
$domain = $_SERVER['HTTP_HOST'];
//find out the path to the current file:
$path = $_SERVER['SCRIPT_NAME'];
//find out the QueryString:
$queryString = $_SERVER['QUERY_STRING'];
//put it all together...
$url = $protocol . $domain . $path . "?" . $queryString;
echo "The current URL is: " . $url;

//An alternative way is to use REQUEST_URI instead of
//both SCRIPT_NAME and QUERY_STRING:
$url2 = $protocol . $domain . $_SERVER['REQUEST_URI'];

echo "The alternative way: " . $url2;