php for C/C++/Java programmers

For this string of tutorials, I will assume

1. Knowledge of basic html

2. Knowledge of a high-level programming language like C.

3. Knowledge of Object Oriented Programming using C++/Java (only for object-oriented php).

4. A wamp or XAMPP installation.

Module 1

Basic User Registration and Login Using php

For starters, you only need to know that in php, variables need not be declared before they are referenced.

PHP automatically finds out the most suitable type for a variable and assigns that type to it.

For example,

You can write

$name=”Punit”; //$name is a string
$value=5; //$value is an integer

 

Let’s get started right away with a simple illustration of how you can register users using php and html.

Html form(form.html)

Open any text editor and type the code below. Save it as form.html in the ‘www’ directory (in case of wamp) or ‘htdocs’ directory (in case of ‘XAMPP’).

<body>
            <form method="get" action="register.php">
            Username:<input type="text" name="username" /><br /> 
            Password:<input type="password" name="password" /><br />
                     <input type="submit" name="submit" value="submit" />
            </form>
</body>

Now, let’s understand the main components of the html form one by one.

GET and POST

GET transfers data to global array $_GET while POST transfers data to global array $_POST. In php, the indexes of an array can be defined by the user.

In C++ or java, the indexes of an array are by default 0,1,2,3…..

But in php, an array can have an index as a string as

$arrayname=array(‘name’=>’Ferrari’);

Thus if you want to reference ‘Ferrari’ you can write

$arrayname[‘name’];

ACTION

After you press the submit button, all the values input in the forms will be sent to register.php

You can now reference those variables using $_GET[‘username’] and $_GET[‘password’].

In case of POST, the values can be referenced as $_POST[‘username’] and $_POST[‘password’].

Register.php

Now, open a new file in your text editor and type or copy the following code in it. Save it as register.php in the same folder as form.html


“<?php"
indicates start of php script and “?>” indicates end of php script.

<?php
$username=$_GET['username'];
$password=$_GET['password'];
echo $username."<br>";   //'.' is the string concatenation operator in php. You can print html code by keeping it inside quotation marks.
echo $password;
?>

You don’t want to ask someone to input their username and password and display it, do you?

Of course you don’t. We’ll go for much better ways to add security to such forms. I just didn’t want to start you off with the traditional “Hello World!”example. It really sets me off why people are still stuck with that boring example and do not go for much real world examples.

If you now look at the url, you can see it as:

http://localhost/register.php?username=’ferrari’&password=’mclaren’

It doesn’t take much time to realize that your username and password are visible even when you do not print them using the php. This can be avoided using ‘’post’’ instead of ‘’get’’. Try replacing get with post in both the html form and the php script and see the difference in the url.

So why does php provide a ‘get’ if it is so unsecure??

Because there are many advantages to using get. I actually lied about data being sent through a global array in case of GET.
To find out why, try putting the following url in the address bar of your browser.

http://localhost/register.php?username=’foo’&password=’oof’

This will give the output as

foooof

You can put any values for username and password in the url and they will get printed accordingly. This shows how get transfers the values to result.php…using additions to the url.

Now you can see how useful GET can be, by adding more and more parameters and values to the url you can send as much information as you want from one page to the other.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: