Archive

Posts Tagged ‘variable’

Redirect HTTPS To HTTP

April 9th, 2009 No comments

To redirect from HTTPS to HTTP on the home page only using the following rule.

RewriteCond %{HTTPS} on
RewriteRule ^/?$ http://%{SERVER_NAME}/ [R=301,L]

The variable %{HTTPS} will be either "on" or "off" and will be enabled even if SSL is not installed on your site. The rule above sees that HTTPS is on and redirects the home page to the HTTP version. You can even chain lots of rules together like this.

RewriteCond %{HTTPS} on
RewriteRule ^/?$ http://%{SERVER_NAME}/ [R=301]
RewriteRule ^inner/directory/?$ http://%{SERVER_NAME}/inner/directory/ [R=301,L]

Note that you should end your last rule with L so that no other rules on the page are run. Also, you need to make absolutely sure that you are not redirecting any pages that are integral to the security of your shopping cart as this will turn off HTTPS for those pages.

You can also do the same thing using the ${SERVER_PORT} variable.

RewriteCond %{SERVER_PORT} 443
RewriteRule ^/?$ http://%{SERVER_NAME}/ [R=301,L]

The port for HTTPS is 443 so if the port being communicated through is 443 we need to redirect.

Getting Started With PHPDoc

January 26th, 2009 No comments

If you followed the tutorial on installing PHPDoc then you are probably wondering how to get started actually writing the documentation. PHPDoc will parse all the files given to it and look for comment blocks, it will then use these blocks to create the documentation for your application. A valid PHPDoc comment block must start with a '/**', have a '*' on every line and finish with '*/'. The comment must appear before the section that is being commented upon and any lines within the comment without a '*' will be ignored by the parser.

To create some sample documentation we will use the following useless class.

<?php
include('another_file.php');
 
class MyClass
{
 
  public $publicVariable = 123;
  private $privateVaraible;
 
  public function aFunciton($parameter)
  {
    // some code here...
  }
 
}

The first thing we need to do is create a file comment, this comment is intended to tell people what this file contains. In this example the file contains a single class definition and an include statement.

/**
 * MyClass File (short description about the file)
 *
 * This file contains the definition of the file MyClass (a longer description about the file).
 * @author A.N. Other
 * @version 1.0
 * @package MyPackage
 */

Notice the use of the @ symbol. This is called a tag and allows PHPDoc to assign values to internal variables. So when we use @version, we are setting the version variable for our file.

define() statements, functions, classes, class methods, and class vars, include() statements, and global variables can all be documented. So the next step is to document what the include statement includes. PHPDoc will attempt to find any files referenced through include or require statements and generate documentation for those files. If present, a link will be created from this file to that file in the documentation.

/**
 * Include the another_file.php file.
 */
include('another_file.php');

Next is our class definition, which consists of two forms of description(short and long) as well as some tags relating to things like what package the class is in.

/**
 * This is a short description about the class.
 *
 * This is a longer description which should detail exactly what the class does.
 * @package test
 * @author A.N. Other
 * @version 1.0
 * @copyright A copyright notice, if any.
 */
class MyClass
{
}

The two variables within the class must also be documented.

/**
 * A description of the variable.
 * @var integer
 */
public $publicVariable = 123;
 
/**
 * This variable will only appear in our documentation is we set the --parseprivate attribute on the command line or it has been set up in the INI file.
 * @access private
 * @var string
*/
private $privateVaraible;

Finally, we document the function. The functions are documented using the two tags of @param and @return. The @param can be defined multiple times and is a descriptive list of the parameters that the function takes. The @return value can only be defined once and describes what the function returns. If more than one datatype is returned from the function then they can be separated by a pipe (|).

/**
 * This class either returns an integer or a string.
 * @param A description about the parameter $parameter
 * @return string|int This function returns a string or an integer.
 */
public function aFunciton($parameter)
{
  // some code here...
}

To add more than one parameter just add another line in that starts with @param.

You can run PHPDoc on this file by using the following command (assuming that your project is kept in a folder called "MyProject" and that you want the documentation to go in "docs" within that directory.

C:\MyProject>phpdoc -t "C:\MyProject\docs" -o HTML:frames:default -d "C:\MyProject"

Work Out Real File Sizes With PHP

June 6th, 2008 No comments

Displaying the size of a file in bytes is all well and good, but means little to most people so converting the size into something a little more readable is a must. The following function will take the number of bytes that a file is and convert it into something a little more meaningful. Rather than just convert the value into kilobytes it returns the largest denomination of size. So if your file is greater than 1073741824 bytes the function will return your value in gigabytes.

function readableFileSize($size){
 $size = $size-1;
 if($size >= 1099511627776){
  return number_format(($size / 1099511627776),2) . ' TB';
 }elseif($size >= 1073741824){
  return number_format(($size / 1073741824),2) . ' GB';
 }elseif($size >= 1048576){
  return number_format(($size / 1048576),2) . ' MB';
 }elseif($size >= 1024){
  return number_format(($size / 1024),2) . ' KB';
 }elseif($size > 0){
  return $size . ' b';
 }elseif($size == 0 || $size == -1){
  return '0 b';
 }
}

Here are some examples of it in action.

echo readableFileSize(512); // 511 b
echo readableFileSize(3793); // 3.70 KB
echo readableFileSize(456765); // 446.06 KB
echo readableFileSize(5000000); // 4.77 MB
echo readableFileSize(123456789); // 117.74 MB
echo readableFileSize(648564967358); // 604.02 GB

This function comes in handy when telling users the maximum size of file they can upload. As this is probably set as a variable in bytes in your script you can’t just print it out to users as they will have to work this out for themselves.

Update

I recently found a very neat way of doing this using a single loop. It is slightly more processor intensive than the original version, but it works by continuously dividing the size by 1024 until it is less than 1024. The number of times this is done is then used to figure out the units.
function readableFileSize($size){
 $units = array(' B', ' KB', ' MB', ' GB', ' TB');
 for($i = 0; $size > 1024; $i++){
  $size /= 1024;
 }
 return round($size, 2).$units[$i];
}

Categories: PHP Tags: , , , , , ,