Friday, March 20, 2015

URL Routing in PHP


How to Implement URL Routing in PHP

Among the major aspects observed by search engines to determine the relevance of a particular page to a specific search term is that whether the URL link itself embraces a certain word or not. The instant ability to define your URLs in a more human readable, SEO friendly format and to remap old URLs to novel functionality is the key reason for organizations to adopt for URL routing.

What is URL Routing?
URL Routing is a mechanism used to map URLs to the code that gets executed only when a certain request is received at the server.
Why you need URL Routing?
URL Routing makes your URLs considerably more meaningful. Suppose, you have a page that searches for books based on the author’s name. It may appear like this:
index.php?action=search&type=book&author=fitzgerald
Such type of URLs are difficult to understand and remember for the user. Therefore, to make these URLs more user friendly, you use ‘URL Routing’ and transform these URLs into simpler format.
/search/book/ftizgerald
Another great advantage of URL routing is found in Search Engine Optimization and there is no denying to the fact that strong SEO can enhance the positioning of your website and eventually profitability.
How to implement URL Routing in PHP?
The following guide will help you in implementing URL routing in PHP and Apache.
  1. First of all, make sure you have installed and enabled rewrite module in Apache’s configuration
  2. Now, we have to create a .htaccess file in root directory of our website
    1
    2
    3
    4
    
        Options -MultiViews
        RewriteEngine On
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteRule ^ index.php [QSA,L]
    * Check, if the mod_rewrite has been enabled or not. If, yes then the following configuration will be used.
    * ‘RewriteEngine On’ enables the rewrite engine. Next, we will handle all the requests that do not correspond to the file names. After that, we will pass the requests to index.php file. Now, index.php will manage all the requests.
  3. For every page request, index.php will be executed. In order to distinguish between the different requests, you will have to use $_SERVER['REQUEST_URI']
  4. Next, simply explode $_SERVER['REQUEST_URI'] with ‘/’ and you will get the substrings that will help you to compare and execute the code corresponding to them.
The Real Code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
 /*
 The following function will strip the script name from URL i.e.  http://www.something.com/search/book/fitzgerald will become /search/book/fitzgerald
 */
 function getCurrentUri()
 {
  $basepath = implode('/', array_slice(explode('/', $_SERVER['SCRIPT_NAME']), 0, -1)) . '/';
  $uri = substr($_SERVER['REQUEST_URI'], strlen($basepath));
  if (strstr($uri, '?')) $uri = substr($uri, 0, strpos($uri, '?'));
  $uri = '/' . trim($uri, '/');
  return $uri;
 }
 
 $base_url = getCurrentUri();
 $routes = array();
 $routes = explode('/', $base_url);
 foreach($routes as $route)
 {
  if(trim($route) != '')
   array_push($routes, $route);
 }
 
 /*
 Now, $routes will contain all the routes. $routes[0] will correspond to first route. For e.g. in above example $routes[0] is search, $routes[1] is book and $routes[2] is fitzgerald
 */
 
 if($routes[0] == “search”)
 {
  if($routes[1] == “book”)
  {
   searchBooksBy($routes[2]);
  }
 }
Conclusion
This is how you will be able to implement minimal URL routing mechanism. There are a plenty of PHP frameworks that provide enhanced URL Routing features and you can try them as well.

Tuesday, February 24, 2015

How To Become A Successful Software Developer: 13 Steps

http://www.techgig.com/tech-news/editors-pick/How-To-Become-A-Successful-Software-Developer-13-Steps-29277?mailer_id=2181&utm_source=Mailer&utm_medium=TG_batch&utm_campaign=digest_news_2015-02-23&email=ndlganeswararao@gmail.com&activity_name=Mjk4NzA=&template_type=3&dt=&auto_login=bmRsZ2FuZXN3YXJhcmFvQGdtYWlsLmNvbUAjJEAjJDUzNDM1M0AjJEAjJDEzODQ0NjE2MTE=&src_type=autoLogin

or

Software Development is no longer considered as an art. But it was made as an engineering practice. A successful software developer is a person who is able to write each line of code with passion. And passion cannot be obtained by any software engineering degree, it must come from within yourself.

1.Be passionate about the being a software developer. If you are not passionate for sitting in front of the PC for the whole day, simply enjoying the lines of code you write; this is not for you.
 
2.Always start with the simple hello world.

3.Don't try to learn all the programming languages. What you need to know is the concepts.

4.Don't start programming with a languages such as C/C++. Try something like Java or C Sharp.

5.When you face an error. Always tell to yourself, "I'm NOT ALONE, Someone in this world should have faced the same error before". And GOOGLE the exact error message. Chances are 99% you will find the answer.

6.Be more public. If you will not show others that you are cool and you deserve more, how will they know? There is simple way to do that - start blogging, ask and answer questions, ensure Google knows something about you. Share your knowledge in your team and project. If you learned something new, why do not share it. You will forget it if will not be trying it.

7.Never use Microsoft notepad to do coding. Use at least Notepad++.

8.When you see an application(web application or windows), tell yourself. If this is an application. Then it's technically possible for me to build a similar one.

9.When you see an application(web application or windows), try to model it in your mind. Always ask yourself, can you build a similar application yourself. If not try to find the place where you think you might get stuck. And try to Google and find how to do it.

10.If you get stuck in a code logic for more than two hours; STOP your work; go out to have a tea or coffee, before trying it again.

11.Be confident. Help others and ask from others. Different people will have different aspects to solve an issue.

12.Never be ashamed, when you find the bug as a single line of code. Usually most of the bugs can be corrected in a single line of code. What matters is, how hard you tried in different aspects to solve the issue.

13.Find a mentor. This does not mean that you a need a person who will help you in doing your job - it is a coach or a more senior staff then you are. This means that you need a person who stays where you want to be and you need to take a leaf from his/her book. And if he will not be high enough - you will need to find another. But all the time you need to have that person. Also have people, friends if you will, who will help you keep on track. It could be your wife or girlfriend , or best friend who always supports you (in my case he is a developer, but this doesn't matter).

Monday, December 29, 2014

Send SMS through Way2sms API

http://pristineseo.com/php-code-sending-free-sms-way2sms-account/

<?php

function send_sms($userID, $userPWD, $recerverNO, $message)
{
    if (!function_exists('curl_init')) {
        echo "Error : Curl library not installed";
        return FALSE;
    }
    $message_urlencode = rawurlencode($message);
    if (strlen($message) > 140) {
        $message = substr($message, 0, 139);
    }
   
    $cookie_file_path = "/var/www/html/cookie.txt";
    $temp_file        = "/var/www/html/temporary.txt";
    $user_agent       = "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.153 Safari/537.36";
   
    // LOGIN TO WAY2SMS
   
    $url        = "http://site24.way2sms.com/content/Login1.action";
    $parameters = array(
        "username" => "$userID",
        "password" => "$userPWD",
        "button" => "Login"
    );
   
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_POST, count($parameters));
    curl_setopt($ch, CURLOPT_POSTFIELDS, $parameters);
    curl_setopt($ch, CURLOPT_HEADER, TRUE);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
    curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file_path);
    curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file_path);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
    curl_setopt($ch, CURLOPT_USERAGENT, $user_agent);
    curl_setopt($ch, CURLOPT_NOBODY, FALSE);
    $result = curl_exec($ch);
    curl_close($ch);
   
    // SAVE LOGOUT URL
   
    file_put_contents($temp_file, $result);

    $result     = "";
    $logout_url = "";
    $file       = fopen($temp_file, "r");
    $line       = "";
    $cond       = TRUE;
    while ($cond == TRUE) {
        $line = fgets($file);
        if ($line === FALSE) { // EOF
            $cond = FALSE;
        } else {
            $pos = strpos($line, ' window.location="');
            if ($pos === FALSE) {
                $line = "";
            } else { // URL FOUND
                $cond       = FALSE;
                $logout_url = substr($line, -25);
                $logout_url = substr($logout_url, 0, 21);
            }
        }
    }
    fclose($file);
   
    // SAVE SESSION ID
   
    $file = fopen($cookie_file_path, "r");
    $line = "";
    $cond = TRUE;
    while ($cond == TRUE) {
        $line = fgets($file);
        if ($line === FALSE) { // EOF
            $cond = FALSE;
        } else {
            $pos = strpos($line, "JSESSIONID");
            if ($pos === FALSE) {
                $line = "";
            } else { // SESSION ID FOUND
                $cond = FALSE;
                $id   = substr($line, $pos + 15);
            }
        }
    }
    fclose($file);
   
    // SEND SMS
   
    $url        = "http://site24.way2sms.com/smstoss.action?Token=" . $id;
    $parameters = array(
        "button" => "Send SMS",
        "mobile" => "$recerverNO",
        "message" => "$message"
    );
   
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_POST, count($parameters));
    curl_setopt($ch, CURLOPT_POSTFIELDS, $parameters);
    curl_setopt($ch, CURLOPT_HEADER, TRUE);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
    curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file_path);
    curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file_path);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
    curl_setopt($ch, CURLOPT_USERAGENT, $user_agent);
    curl_setopt($ch, CURLOPT_NOBODY, FALSE);
    $result = curl_exec($ch);
    curl_close($ch);
    // LOGOUT WAY2SMS
   
    $url = "site24.way2sms.com/" . $logout_url;
   
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_HEADER, TRUE);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
    curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file_path);
    curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file_path);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
    curl_setopt($ch, CURLOPT_USERAGENT, $user_agent);
    curl_setopt($ch, CURLOPT_NOBODY, FALSE);
    $result = curl_exec($ch);
    curl_close($ch);
    // DELETE TEMP FILES
   
    unlink($cookie_file_path);
    unlink($temp_file);
   
    return TRUE;
   
}
?>