Showing posts with label php. Show all posts
Showing posts with label php. Show all posts

Thursday, July 18, 2013

Developing an eMail validation system using PHP and MySQL

Well, its great that we do create websites that require sign up from users. To prevent random sign ups and span, developers have developed an email verification system. This system can be seen in almost every website throughout the WWW. In this article, I am going to show you how you can develop a simple script that validates email address.
Firstly, let me explain the database structure:
The name of the database is test, and the name of the table is email. The field email is considered unique. The activatecode contains the corresponding activation code for email generated while the user was signing up. The field activatestatus is self explanatory. It contains one character either Y or N to denote the activation status.

Now, recipe that we are going to use to cook this script is the HTTP GET. PHP can read the GET instruction from URL and convert it into an associative array. The same thing we are going to use in this tutorial.
Let's Code!
//Initializing database connection starts
$host = 'localhost';
$user = 'test';
$password = 'test';
$database = 'test';
$con = mysqli_connect($host, $user, $password, $database);
if(!$con)
    {
    echo mysqli_error ($con);
    }
//connected to the database
//you can check if URL is in the correct format or not before whatever we are going to do here
$key = $_GET['key']; //key will be the URL parameter
$email = $_GET['email'];//for security, we will also consider email as the paramaeter
$query = "Select * from email where email='$email'";
$result = mysqli_query($con, $query);
$assoc = mysqli_fetch_assoc($result);//an associative array is fetched for better understanding
//as email is a unique field it will only fetch one 1D array
if($key = $assoc['activatecode']){
    $query = "update email set activatestatus='Y'";
    $update = mysqli_query($con, $query);
    if(!$update) echo mysqli_error ($con);
    else echo 'activation successful';
}
else echo 'Either the Email is wrong or the associated key is not with the email';
mysqli_close($con);

Now that we have coded, here's how to change the activatestatus of a given email. Just point your URL to
http://---path-to-your-activation-script--.php?email=john@example.com&key=564186451684865

You email will be activated.
Download this script!
More coding at Let's Code
Warning: This is for testing purposes only. I am giving you this tutorial as an advanced example on how to use several functions in PHP and running appropriate querys in MySQL using PHP. Do not publish!

Sunday, July 14, 2013

Creating own URL Shortner

Well, we have seen many websites like bit.ly, goo.gl, and also the $$ earning adfoc.us and adf.ly. Ever wondered how these systems work?
Well, I don't know how exactly do they work, but I can show you how to create something like those websites.
The concepts that we will be using to create own URL shortner are: MySQL access functions in PHP, and the header function in also, PHP.

First we need to set up a MySQL database containing two columns more importantly. Here is the SQL query for the same
CREATE TABLE shorturls
(
short_url varchar(6),
long_url varchar(256),
);

Now, we have a table with two columns with the list of the short URL codes and the corresponding long URL. For this instance, I will be using the URL code "tekish" and it will point to http://techieanish.blogspot.com. Don't worry, we will be talking about generalizing the data. That is every time a URL code is gonna fire, it will redirect to corresponding long URL. So, now as we are done with creating tables, we are now gonna set up the PHP script that will fire when you visit the code. For this, first we will need to do some nice good setting. As we are just dealing with the basic concepts, the sweetness of this desert will be the 404 redirect. To do this, follow these steps:
  1. If you are using Apache as your server, save a file called ".htaccess" in the main directory of your website. The directory in which your index.php lies.
  2. Now, in this directory, enter the following: ErrorDocument 404 /redirect.php
Now we are going are going to write the code for redirect.php. Well, we did the htaccess thing so that whenever some random piece of string goes behind your web domain name, a 404 error occurs. Now, to show that 404 error, we fire up the redirect.php, that is basically a redirecting PHP script.
Now lets do the real thing. Code redirect.php

Basic notes and additional tips:

  •  In handling the .htaccess file, make sure your correctly enter the address of the location of your redirection script. Make sure you enter the path as on server, not as your local domain.
  • There are lot of things I haven't included in the script, which include analytics, form to create new URLs, and an error page if the code is not found. Explore them on your own! 
  • Well, when you are starting a service, you will obviously need to set up a random code generator for new URLs. Here is a small example on how you can create random alphanumeric codes that can be used as your short URLs.
  • Also, trim the URL you get to alphanumeric code for security
  • Got more tips? Share them in comments.
More coding at Let's Code!


Saturday, July 6, 2013

PHP Code Snippets: Generate a random string

Generating random strings might be very useful when developing a website. You can use random strings for various purposes:
2) Generating Coupon codes for a shopping website
And lots more! 
Soon, I will add tutorials to create all the examples that I have mentioned above. But for now, just take a look at the function I have given below:

The variable $length can be set to the length of string that you want to have.

How to use the function?

Just call it whenever you want the random string to interact with the PHP script you are designing.
Ex: $myrand = generatestring(8) // Generates 8 character random string

Explore!

Add more type of characters you wanna include in your PHP script to the variable in the function $charset

More coding at Let's Code.


Saturday, June 22, 2013

How to run a MySQL query using PHP

Simple tutorial!
Prerequisite Knowledge: Basic PHP, and MySQL
Prerequisite Stuff: A server that can run both PHP and MySQL(here is a simple way to set up the same on your system)

Step 1: Create a MySQL conenction!

  1. Define 3 string variables to define, the database host name, username and password
    • $dbhost = 'localhost'; //enter your hostname under quotes;
    • $dbuser='myuser'; //enter your database user name in the quotes;
    • $dbpass='mypassword; //enter your database user password;
  2. Create a MySQL connection using mysql_connect() function
    • $connection = mysqli_connect($dbhost,$dbuser,$dbpass);
    • if(!$connection) echo 'cannot connect to database'; //Checks whether connection is made

Step 2: Select the required database

  1. Define a string variable with database name:
    • $dbname='mydatabse' // Enter your name of database under quotes
  2. Select the databse using mysql_select_db() function
    • $select = mysqli_select_db($connection,$dbname) or die('cannot select database');
      • The die function checks whether the database could be selected or not

Step 3: Lets run the query

  1. Define a string variable for query.
    • $query = 'SELECT * FROM table'; //put your MySQL query in quotes
  2. Run the query using mysql_query() function.
    • $query = mysql_query($query)
    • We define the variable to get the result from MySQL.
Enjoy! Happy coding. Practice the code by understanding it. Yup, I did not include a sum up, understand and code it yourself! You can do it, trust me!

More coding at Let's Code!
Edit: Modified with mysqli extension. 

Thursday, June 20, 2013

Best books to kickstart learning web development

A complete beginner to programming and web development? Need something to kick start your learning of the languages that will let you convert your ideas into full featured web apps? Here are three books I recommend that you should start with. Sure you can find PDFs for the same, but I highly recommend you to buy these books, as they have lot of pencil-driven exercises as a part of their course. Also, they incorporate funny methods to keep you interested all time long. In fact, all of us thought that learning programming was just about reading texts and texts, I sure did, but this is certainly something new! They have lots of graphics and hilarious methods of understanding the cores of any language they publish a book about.
Learning PHP:




Both links above have the lowest price that you are gonna ever find on the market. Enjoy!

Set up the best web development environment - Netbeans + Apache + PHP + MySQL + jQuery and lots more!


Well, PHP, MySQL, jQuery - the three most essential tools we need to create the next gen websites or develop interactive content in the world of HTML5.
jQuery will run client side, but PHP and MySQL require server side programming. Hence we need to set up a similar environment on PC, and also we need something to code our thoughts and ideas using our knowledge. Netbeans is one of the best IDE(Integrated Development Environment) that can let you do this with much ease, and basically its free. Everything that you will need to complete this tutorial will be free.

Step 1: Set up a server

There are various apps that let you create an Apache+PHP+MySQL environment of your Windows PC. My favorites are:
Software Download Link Extra notes
Zend Server Click here More secure, High level server administration, might not have the latest version of PHP, MySQL and other modules
EasyPHP Click here Less Secure, Easy administration and installation of modules. Fast updates to PHP and MySQL << Recommended 
WampServer Click here Less secure,option to convert to web server, might not have the latest version of technologies(Apache, PHP, MySQL)

Well, just install these apps like you install any other Windows Software and your server will be up and running in no time.
This was easy, wasn't it?
Now comes the coding part..

Step 2: Install Netbeans

Why Netbeans?
Well, its fully featured, easy to use and has support for lots of languages and is FREE! You will also need JDK(Java SE) latest version. Here is the link.

Step 3: To create projects using NetBeans

Well, creating projects is easy. Now what you need is a simple selection to make when you are kick starting your development. In the New Project Wizard, choose PHP>PHP Application.

If you are creating a PHP project for the first time, Netbeans will take few seconds to activate the PHP Module.
 When you are in this screen when going through the New Project wizard, make sure you check the box copy files to a folder option as mentioned below. 
Now, select the path according to the location of your public www folder(also called document root). If you have downloaded any of the above softwares that I have mentioned, here is a guide to those folders:
ZendServer - <path to Zend Server installation directory>\Apache2.2\htdocs
EasyPHP - <path to EasyPHP installation directory>\data\localweb
WampServer - <path to WampServer installation directory>\www

Thats it! You are done!

Additional Notes:

  1. I prefer EasyPHP as they have latest technologies, so less or no bugs will be encountered when developing.
  2. Do set up root password for your MySQL databases. This makes your server quite secure, in case you decide to make the same server live.
  3. Copy the jQuery script into the folder where you can use it properly. For less amount of code, I prefer to save the .js file into the same directory as the main web page is.
  4. It is always better to choose a proper web host, rather than hosting the scripts yourself. Sure this is a DIY package for creating a web server, but its not secure enough.
  5. WAMPServer comes with phpMyAdmin by default, to access it use localhost\phpmyadmin. For ZendServer and EasyPHP you will have to install the modules.For tutorial on Zend Server, visit this link and for EasyPHP, just activate the phpMyAdmin module in the server administration GUI.
Now that you have set up a complete web Development server, how about kickstarting to learn web development? Check out one of the best books I recommend for beginners to start learning web development by clicking here!