Developers Archive for the 'Advanced Php Issues' Category

Clean URLs without mod_rewrite

Clean URLs without mod_rewrite Wednesday, April 25th, 2007

Introduction
As you may know, clean URLs can be made with Apache’s mod_rewrite, but you are able to make clean URLs with PHP only.

Contents
This tutorial is split up into the following parts:

  • Introduction
  • Method one
  • Implementing method one
  • Method two
  • Implementing method two
  • Conclusion

  • Method one

    The second method will take /users/2 and it’s purpose is to go to the users page on page 2.


    <?php
    $array = explode('/',$_SERVER['PATH_INFO']);
    array_shift($array);
    ?>

    PHP contains several predefined variables, one of them is $_SERVER[’PATH_INFO’]. To know what the first line does you must know the explode function. The explode function takes two arguments. The first is the seperator and the second is the string. The explode function takes the string and splits it up where the seperator is.

    array_shift removes the first key of an array.

    Implementing method one
    First you should put the above snippet in a file called something like clean_urls.php and include it on the top of your pages (or if you only have one page, just put it in the top).

    Here is how you could use it:


    <?php
    include 'clean_urls.php'; // Include the snippet
    $modules = array( // Define our array of valid modules
    ‘home’,
    ‘users’,
    ‘login’,
    ‘register’,
    ‘admin’,
    );

    $module = $array[0]; // The first key contains what we will use as our module
    $page = $array[1]; // The second key contains what we will use as the page number

    if(empty($module)) // We need a default module
    {
    $module = “home”;
    }

    if(in_array($module,$modules)) // Does the requested module exist?
    {
    include “./modules/{$module}.php”; // If yes: Include it
    }
    else {
    die(”Failed to load module ‘{$module}’”); // If no: Error message
    }
    ?>

    Then you would use the page variable for something useful in your module page.


    Method two

    The second method will convert /func/users/page/2 into what ?func=users&page=2 would have done.


    <?php
    $array = explode('/',$_SERVER['PATH_INFO']);
    for($i=1; $i

    Here the first line is the exact same line as in method one.

    Next we have a for loop. It starts by setting $i to 1, then it will run as longs as the second statement ($i<=count($array)-1) evaluates to true and by the end of each iteration it will add 2 to $i. Inside the loop we add to the $_GET variable.

    Implementing method two
    Method two is easy to implement. Just put the above snippet in a file called something like clean_urls.php and include it on each page.

    Conclusion
    As you see, clean URLs are very easy to make with PHP only. With only 2-4 lines, you can have clean URLs.

    Read your e-mail with PHP!

    Read your e-mail with PHP! Monday, March 19th, 2007

    To retrieve the e-mails in our inbox we will have to use the POP3 protocol, and the raw PHP socket functions, such as fsockopen. But why should we all write it from scratch, when there are already many great libraries available?

    That’s why we’re going to use a POP3 class from PHPClasses.org, available at http://www.phpclasses.org/browse/package/1120.html. Unfortunately, you’ll need to login to download the package, but registration doesn’t take long.

    Once you’ve downloaded the class, try the following example:
    <?php
    require (’pop3.class.inc’);

    $pop3 = new POP3;

    // Connect to mail server
    $do = $pop3->connect (’YOUR-EMAIL-SERVER-HERE);
    if ($do == false) {
    die($pop3->error);
    }

    echo ‘Connected to mail server’;

    $pop3->close();

    ?>

    In the above example you will have to put in your own mail server. If you don’t know what your own mail server is, try something like mail.YOURDOMAIN.com or mail.YOURISP.com.

    If everything goes okay, you will see ‘Connected to mail server’ when running the example. This means that the script was able to connect to your mail server, and the next step is to login to your e-mail inbox.

    This can be done with the login() method, and you will have to pass your username (which is usually your e-mail address) and your password, like this:
    // Login to your inbox
    $do = $pop3->login (’email@address.goes.here.com, ‘password-here’);

    if ($do == false) {
    die($pop3->error);
    }

    echo ‘
    Logged in’;

    Now that we’ve logged into our inbox, we’re almost ready to start downloading new e-mail messages, but first we have to get the ‘office status’, which basically tells our script how many new e-mails there are.

    The office status can be retrieved with the get_office_status() method; see the example below:
    // Get office status
    $status = $pop3->get_office_status();

    if ($status == false) {
    die($pop3->error);
    }

    $count = $status[’count_mails’];

    echo ‘There are ‘ . $count . ‘ new e-mails waiting for you!’;

    As you can see in the above example, the get_office_status() method returns an array, which includes the number of new e-mails, but also a few other details, such as the total size of the new e-mails. This will allow you to create a neat looking progress bar.

    Now we can start retrieving the new e-mails with a for-loop and the get_mail() method, like this:
    for ($i = 1; $i <= $count; $i++) {
    $email = $pop3->get_mail($i);

    if ($email == false) {
    echo $pop3->error;
    continue;
    }

    echo ‘<pre>’;
    print_r ($email);
    echo ‘</pre>’;
    }

    If you run the above example, it will print every new e-mail in your inbox, including the headers of each e-mail.

    Colorfade

    Colorfade Tuesday, February 13th, 2007

    first var is the start color (in hex - FFFFFF)

    second is the finishing fade color (same hex format).

    third var is the string. So if you want to fade from red to green “PHPFreaks Rocks!” you would put:

    colorfade_string(”FF0000″, “00FF00″, “PHP Rocks!”);

    function colorfade_string($hex_start, $hex_end, $string)
    {
    $realLen = trim(strlen($string));

    //we need to skip spaces
    $n_spaces = substr_count($string, ” “);

    $len = $realLen - $n_spaces;

    if($len == 0)
    die(”String length = 0″);

    if(!ereg(”([0-9A-Fa-f]{2})([0-9A-Fa-f]{2})([0-9A-Fa-f]{2})”, $hex_start, $firstcolor))
    die(”First Color is Invalid”);

    if(!ereg(”([0-9A-Fa-f]{2})([0-9A-Fa-f]{2})([0-9A-Fa-f]{2})”, $hex_end, $endcolor))
    die(”Second Color is invalid”);

    $start_1 = hexdec($firstcolor[1]);
    $start_2 = hexdec($firstcolor[2]);
    $start_3 = hexdec($firstcolor[3]);

    $end_1 = hexdec($endcolor[1]);
    $end_2 = hexdec($endcolor[2]);
    $end_3 = hexdec($endcolor[3]);

    $diff1 = $end_1 - $start_1;
    $diff2 = $end_2 - $start_2;
    $diff3 = $end_3 - $start_3;

    $step1 = round($diff1 / $len);
    $step2 = round($diff2 / $len);
    $step3 = round($diff3 / $len);

    $red = $start_1;
    $blue = $start_2;
    $green = $start_3;

    for($i = 0; $i < $len; $i++)
    {
    $hex[] = sprintf(”%02X%02X%02X”, $red, $blue, $green);

    $red += $step1;
    $blue += $step2;
    $green += $step3;
    }

    for($i = 0, $k = 0; $i < $realLen; $i++)
    {
    $char = substr($string, $i, 1);

    if($char != ” “)
    {
    print(’<font color=”‘ . $hex[$k] . ‘”>$char</font>’);
    $k++;
    }
    else
    print(” “);

    }

    }


    All material @ copyrighted by chrisranjana.com. If you want to link to this article you are welcome to do so. Unauthorized publication is strictly prohibited. This developer tutorial website contains articles by Php programmers , Software developers, Mysql programmers and asp c# programmers. This website also contains ajax tutorials and advanced mysql sql stored procedures and functions tutorials and sample codes.