Showing posts with label PHP. Show all posts
Showing posts with label PHP. Show all posts

Monday, 22 February 2016

How to install PHP and MySQL in LINUX

PHP, MySQL and Apache are most important for web developers. Here i am going to show you how to install all of these in your LINUX machine. So let's start and follow my steps carefully.

Step 1 : First of all we need to install apache or any other web server. here we will install apache. Apache is a free open source software which runs over 50% of the world’s web servers. For installing apache on your machine open the terminal in your LINUX machine and type following commands one by one.

sudo apt-get update
sudo apt-get install apache2

Now open your browser and type "localhost" or "". It will show you the following page.

Step 2 : Now install MySQL on your machine. To install MySQL, open terminal and type in these commands.

sudo apt-get install mysql-server libapache2-mod-auth-mysql php5-mysql

In the middle of your installation it will ask you set the root password, If you missed then set the password later within the MySQL shell.
Once you have installed MySQL, we should activate it with this command: 

sudo mysql_install_db

Now type the following command:

sudo /usr/bin/mysql_secure_installation

After this a prompt will ask you for entering the current root password. Type the password. Now prompt will ask you to change the password, So type N.
After that type 'Y' in all upcoming prompts. At last MySQL will reload and implement the new changes. 
Now MySQL installed successfully. For checking it open the terminal and type the following command :

sudo mysql root your_password

Step 3 : Now we are going to install PHP. To install PHP, open terminal and type in this command.

sudo apt-get install php5 libapache2-mod-php5 php5-mcrypt

Type 'Y' in two prompts after it. It will install PHP automatically.
Now add php to the directory index by the following command:

sudo nano /etc/apache2/mods-enabled/dir.conf

Now add the index.php in beginning of the index files. It should be see like this :

<IfModule mod_dir.c>

          DirectoryIndex index.php index.html index.cgi index.php index.xhtml index.htm


Now type the command for adding the libraries and modules:

apt-cache search php5-

The terminal will show you the all modules. Select one of them and type the command:

sudo apt-get install name of the module

Now setup is almost complete. create a new file named "phpinfo.php" in '/var/www' folder and type the following lines.


Save it and open the browser and type "localhost/phpinfo.php"  the following address in address bar. It will show you the following screen.

Good luck guys. 

Monday, 13 April 2015

Restrict Filesystem Access to a Specific Directory

If your application must operate on the filesystem, you can set the open_basedir
option to further secure the application by restricting access to a specific directory.If
open_basedir is set in php.ini, PHP limits filesystem and I/O functions so that they
can operate only within that directory or any of its subdirectories. For example:

open_basedir = /some/path

With this configuration in effect, the following function calls succeed:


But these generate runtime errors:

$fp = fopen ("/some/other/file.exe", "r");
$dp = opendir("/some/path/../other/file.exe");

Of course, one web server can run many applications, and each application typically
stores files in its own directory.You can configure open_basedir on a per-virtual host
basis in your httpd.conf file like this:

DocumentRoot /web/sites/domainA
php_admin_value open_basedir /web/sites/domainA

Similarly, you can configure it per directory or per URL in httpd.conf:

# by directory
<Directory /home/httpd/html/app1>
php_admin_value open_basedir /home/httpd/html/app1
# by URL
<Location /app2>
php_admin_value open_basedir /home/httpd/html/app2

The open_basedir directory can be set only in the httpd.conf file, not in .htaccess files, and you must use php_admin_value to set it.

Thursday, 9 April 2015

How to use PHP to access a Database

Databases are backbone of today's websites. PHP has support for over 20 databases. MySQL, PostgreSQL and Oracle are widely used databases. Databases are used for storing users registration data, credit card numbers, purchase history, product reviews and many other things.
PHP and phpmyadmin both are used to access databases. Relational database management system is a server which is used to manage data. Data is structured into tables where each table have some no. of columns, each of which has a name and a type. PHP communicates with relational databases such as MySQL and Oracle using structured query language (SQL). The syntax for SQL is divided into two parts. The first, Data Manipulation Language, or DML, is used to retrieve and modify data in an existing database. DML is remarkably compact, consisting of only four verbs: select, insert, update, and delete. The set of SQL commands, used to create and modify the database structures that hold the data, is known as Data Definition Language, or DDL. The syntax for DDL is not as standardized as that for DML, but as PHP just sends any SQL commands you give it to the database, you can use any SQL commands your database supports.

Assuming you have a table called movies, this SQL statement would insert a new row:
INSERT INTO movies VALUES(0, 'fast and furious 7', 2015, 2)
This SQL statement inserts a new row but lists the columns for which there are values:
INSERT INTO movies (title, year, actor) VALUES ('hercules', 2015, 2)
To delete all movies from 2015, we could use this SQL statement:
DELETE FROM movies WHERE year=2015
To change the year for hercules to 2014, use this SQL statement:
UPDATE movies SET year=2014 WHERE title='hercules'
To fetch only the movies made in the 2010s, use:
SELECT * FROM movies WHERE year >= 2010 AND year < 2015 .

 Example :  

This example will show you how to insert form data into database using PHP.

1) First create a html form.

2) Now create a connect.php file.

Submitting and Retrieving Forms data using PHP

Web forms are GUI element wherein the data to be processed is submitted to the web server. The whole process is called client/server data processing model. It is applicable in web sites and web applications. Web form contains text boxes, buttons, check boxes, which allows the user to enter the required information. Web form contains a submit button, when you clicked the submit button, the contents of the form is submitted to the server for processing.
Form is formed using <form> tag which having attributes like action, method, enctype and name. All attributes having their own functions, for example enctype is used for making encryption for form contents.

Submitting the form data

The data of the form cab be sent to the server using either the post() or get() method. The get() method having  attribute method equal to get() and post method having attribute method equal to post().
For example : <form name ="amar" method ="get" action="amar.php">.
Note : In post method information submitted by the user don't show whereas in get information entered by the user Shows in url.

Retrieving Form data

The form data retrieve using php file(amar.php. You must have a web server like apache or wampserver and the both HTML and PHP files should be in same directory(root directory) of the server. The server is used for transmitting data and retrieving data. Lei's see how we can do it.
First make a form in HTML file.
<title>Data transmission</title>
<form name="amar" method="get" action="amar.php">
First name:
<input type="text" name="first" />
Last name:
<input type="text" name="last" />
<input type="submit" value="submit" name="button">


Now retrieve data using these lines.

<title>retrieving data</title>
first name is :
echo $_GET["first"];
last name is :
echo $_GET["last"];

It's all about HTML and PHP form data transmission.In next article we will focus on Database connection using PHP.



PHP(Hypertext Preprocessor) is a server side scripting language that is used to develop dynamic web pages. PHP also have capability to communicate with dayabase. PHP free software released under PHP License, they don't contains GNU norms.
There are several versions of PHP released:

1)PHP version 2 : Released on june 8,1995.
2)PHP version 3: Released on 1997.
3)PHP version 4: Released on May 22, 2000.
4) PHP version 5: Released on July 13, 2004.
5)PHP version 6: this is under development.

Features of PHP :
1) Access control : Provides built in web based configuration screen to handle access control configuration.
2) File upload support : Allow users to upload files to a web server. PHP provides the multipurpose internet mail extension decoding process to upload the files onto the server.
3) HTTP based authentication control : Allows the user to create customized HTTP-based authentication mechanism for the web server.
4) Conditional statements and loops : Allow user to work with loops and conditional statements same as C language.
5) Extended regular expressions : Supports all regular expression operations. They are mainly used for pattern matching, pattern substitution and general string manipulation.
6) Access logging : Allow user to record the number of times website  or web page accessed.
7) Safe mode support : Allow multiple users to run php scripts on the same server simultaneously.
8) Open source : Allow user to work with multiple software development languages.
9) Third party application support : PHP also supports third party appliactions like MYSQL database etc.
10) Extensible architecture  : Allow the user to read and write in various formats, such as GIF, JPEG, PNG. Sends and receive emails using SMTP, IMAP, POP3.PHP also allow user to access C libraries, java classes.

PHP code can be embedded in HTML and XHTML in three ways :
1) XML style : <?php
                        //php code
2) Short style : <?
                         //php code
3) Script style : <script language ="php"> 
                           //php code

Creating PHP script : PHP scripts are plain text files containing PHP instructions. Let's see how to create PHP script with HTML:

<head><title>first php</title>
echo "this is my first PHP.";

output will be:

We should remember that for running PHP scripts we must have PHP installed on system.

Official link for PHP  download

How to install PhpMyAdmin

Phpmyadmin is used for database management.There are several other software which we need for using it. 
  1. One server (apache, wampserver).
  2. Mysql database.
  3. PHP environment.
  4. Phpmyadmin 4.3 or later.

1) First install Mysql databse on system.

2) After that install apache in system drive in a self made folder.You can named it as you want.

3) After it extract PHP in that folder.

4) After that change file into php.ini .

5) After that extract phpmyadmin zip file into apache/htdocs folder.

6) After that copy phpinfo.php file from phpmyadmin folder into apche /htdocs.

7)  After that open the browser and type "localhost/phpinfo.php". You will see the follwing  window :

PHPMYADMIN description   


PHP - What is it?

PHP stands for PHP Hypertext Preprocessor.

Taken directly from PHP's home,, "PHP is an HTML-embedded scripting language. Much of its syntax is borrowed from C, Java and Perl with a couple of unique PHP-specific features thrown in. The goal of the language is to allow web developers to write dynamically generated pages quickly. This is generally a good definition of PHP. However, it does contain a lot of terms you may not be used to. Another way to think of PHP is a powerful, behind the scenes scripting language that your visitors won't see. When someone visits your PHP webpage, your web server processes the PHP code. It then sees which parts it needs to show to visitors(content and pictures) and hides the other stuff(file operations, math calculations, etc.) then translates your PHP into HTML. After the translation into HTML, it sends the webpage to your visitor's web browser.

PHP - What's it do?

It is also helpful to think of PHP in terms of what it can do for you. PHP will allow you to:

• Reduce the time to create large websites.
• Create a customized user experience for visitors based on information that you have gathered from them.
• Open up thousands of possibilities for online tools. Check out PHP - HotScripts for examples of the great things that are possible with PHP.
• Allow creation of shopping carts for e-commerce websites.

What You Should Know

• HTML - Know the syntax and especially HTML Forms.
• Basic programming knowledge - This isn't required, but if you have any traditional programming experience it will make learning PHP a great deal easier.

PHP Cookies - Background

Cookies have been around for quite some time on the internet. They were invented to allow webmaster's to store information about the user and their visit on the user's computer.
At first they were feared by the general public because it was believed they were a serious privacy risk. Nowadays nearly everyone has cookies enabled on their browser, partly because there are worse things to worry about and partly because all of the "trustworthy" websites now use cookies.
This lesson will teach you the basics of storing a cookie and retrieving a cookie, as well as explaining the various options you can set with your cookie.

Creating Your First PHP Cookie

When you create a cookie, using the function setcookie, you must specify three arguments. These arguments are setcookie(name, value, expiration):
1. name: The name of your cookie. You will use this name to later retrieve your cookie, so don't forget it.
2. value: The value that is stored in your cookie. Common values are username(string) and last visit(date).
3. expiration: The date when the cookie will expire and be deleted. If you do not set this expiration date, then it will be treated as a session cookie and be removed when the browser is restarted.

//Calculate 60 days in the future
//seconds * minutes * hours * days + current time
$inTwoMonths = 60 * 60 * 24 * 60 + time();
setcookie(lastVisit, date("G:i - m/d/y"), $inTwoMonths);

Retrieving Your Fresh Cookie

$visit = $_COOKIE['lastVisit'];
echo "You've got some stale cookies!";
echo "Your last visit was - ". $visit;

This handy script first uses the isset function to be sure that our "lastVisit" cookie still exists on the user's PC, if it does, then the user's last visit is displayed. If the user visited our site on February 28, 2008 it might look something like this:

Your last visit was - 11:48 - 02/28/08