09 March 2011

How to encrypt decrypt url or code in php

When developing web applications sometimes we encounter situations where we need to encrypt/decrypt url or some code or some information. For such situations I wrote a simple php script using the default php functions:
  • base64_encode()
  • base64_decode()
Now the use of these functions is quite simple. For example if you want to pass some critical information like password in the url like:
http://www.example.com/test.php?password=skills2earn
Then you can encode it using the following code:
http://www.example.com/test.php?password=<?php echo base64_encode('skills2earn'); ?>
The output of the above code will be:

http://www.example.com/test.php?password=c2tpbGxzMmVhcm4=

Now when you want to decode/decrypt it you can do it using the following code:
<?php
echo base64_decode('c2tpbGxzMmVhcm4=');
?>
The output of the above code will be:
skills2earn

Well guyz that was a very simple php encryption and decryption technique, if you have any queries or suggestions, please feel free to comment.

04 March 2011

How to make your own captcha using php

Forms are very common in web based applications. They can be used for signup, signin, posting and etc but a simple macro or a bot can spam on the site using these forms. So the easiest way to stop spam is to introduce a captcha in the web form. There are many open source captcha scripts available on the internet for free but sometimes these capctha's start annoying the user. So I wrote a very simple and user friendly captcha script which I will be showing you guys in this article.

My captcha script can be implemented in two steps:

Step1: Code for HTML Form
In the first step you need to enter a piece of code in your html form, in order to make the implementation of this captcha script easier I am using a dummy HTML form. The code is as follows:
<form action="" method="post" enctype="multipart/form-data" name="form1" id="form1">
  <table width="100%" border="0">
    <tr>
      <td>Select Name:</td>
          <td><label>
          <input type="text" name="test1" id="test1" />
          </label></td>
        </tr>
       <tr>
      <td>Select Email:</td>
          <td><label>
          <input type="text" name="test2" id="test2" />
          </label></td>
        </tr>
        <tr>
          <td colspan="2">Spam Check <?php
        $num1=rand(1,9); //Generate First number between 1 and 9
        $num2=rand(1,9); //Generate Second number between 1 and 9
        $captcha_total=$num1+$num2;
        echo $num1." + ".$num2;
        ?>=
            <label>
              <input name="captcha_entered" type="text" id="captcha_entered" size="5" maxlength="2" />
          </label><input type="hidden" name="captcha_total" id="captcha_total" value="<?php echo $captcha_total; ?>" /></td>
    </tr>
        <tr>
          <td colspan="2"><label>
            <input type="submit" name="button" id="button" value="submit" />
          </label></td>
        </tr>
  </table>
</form>

02 March 2011

Php File Uploader Script

Uploading a file to the server is very common and have become a essential part of any sophisticated web application. So in this article I am sharing the simplest php file uploader script, I will also show you guys the implementation of this php file uploader script. In order to make the implementation of this script easier, I am dividing the implementation of this file uploader script in three steps:

First Step: Taking file from user through an HTML form
In the first step we need to create an HTML form through which the user can upload his file. I am creating a sample HTML form, you can use any of your own, but dont forget to change "enctype" to "multipart/form-data". Changing the "enctype" is mandatory for uploading files.
<form action="" method="post" enctype="multipart/form-data" name="form1" id="form1">
  <table width="100%" border="0">
    <tr>
      <td>Select File:</td>
          <td><label>
          <input type="file" name="file" id="file" />
          </label></td>
        </tr>
        <tr>
          <td colspan="2"><label>
            <input type="submit" name="button" id="button" value="Upload" />
          </label></td>
        </tr>
  </table>
</form>