How to get current page URL in PHP?

To get the current page URL, PHP provides a superglobal variable $_SERVER. The $_SERVER is a built-in variable of PHP, which is used to get the current page URL. It is a superglobal variable, means it is always available in all scope.

If we want the full URL of the page, then we’ll need to check the protocol (or scheme name), whether it is https or http. See the example below:

<?php  
    if(isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on')   
         $url = "https://";   
    else  
         $url = "http://";   
    // Append the host(domain name, ip) to the URL.   
    $url.= $_SERVER['HTTP_HOST'];   
    
    // Append the requested resource location to the URL   
    $url.= $_SERVER['REQUEST_URI'];    
      
    echo $url;  
  ?>   

Output

How to get current page URL in PHP

Note: The isset() function is used here to check whether HTTPS is enabled or not. It checks whether a variable exists or not.

Or, we can also get the full URL of current page using another way given in the next example.

     

<?php  
$protocol = ((!emptyempty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != 'off') || $_SERVER['SERVER_PORT'] == 443) ? "https://" : "http://";  
$CurPageURL = $protocol . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];  
echo "The URL of current page: ".$CurPageURL;  
?>

Output

How to get current page URL in PHP 1

To get only name of the current page opened at browser, see the below example:

  

Output

How to get current page URL in PHP 1

phpMyAdmin

phpMyAdmin is an open-source software tool introduced on September 91998, which is written in PHP. Basically, it is a third-party tool to manage the tables and data inside the database. phpMyAdmin supports various type of operations on MariaDB and MySQL. The main purpose of phpMyAdmin is to handle the administration of MySQL over the web.

It is the most popular application for MySQL database management. We can create, update, drop, alter, delete, import, and export MySQL database tables by using this software. phpMyAdmin also supports a wide range of operation like managing databases, relations, tables, columns, indexes, permissions, and users, etc., on MySQL and MariaDB. These operations can be performed via user interface, while we still have the ability to execute any SQL statement.

phpMyAdmin is translated into 72 languages and also supports both RTL and LTR languages so that the wide range of people can easily use this software. We can run MySQL queries, repair, optimized, check tables, and also execute other database management commands. phpMyAdmin can also be used to perform administrative tasks such as database creation, query execution.

phpMyAdmin is a GUI-based application which is used to manage MySQL database. We can manually create database and table and execute the query on them. It provides a web-based interface and can run on any server. Since it is web-based, so we can access it from any computer.

Features of phpMyAdmin

phpMyAdmin supports several features that are given below:

  • phpMyAdmin can create, alter, browse, and drop databases, views, tables, columns, and indexes.
  • It can display multiple results sets through queries and stored procedures.
  • phpMyAdmin use stored procedure and queries to display multiple results sets.
  • It supports foreign keys and InnoDB tables.
  • phpMyAdmin can track the changes done on databases, views, and tables.
  • We can also create PDF graphics of our database layout.
  • phpMyAdmin can be exported into various formats such as XML, CSV, PDF, ISO/IEC 26300 – OpenDocument Text and Spreadsheet.
  • It supports mysqli, which is the improved MySQL extension.
  • phpMyAdmin can interact with 80 different languages.
  • phpMyAdmin can edit, execute, and bookmark any SQL-statements and even batch-queries.
  • By using a set of pre-defined functions, it can transform stored data into any format. For example – BLOB-data as image or download-link.
  • It provides the facility to backup the database into different forms.

Advantage of phpMyAdmin

  • phpMyAdmin can run on any server or any OS as it has a web browser.
  • We can easily create, delete, and edit the database and can manage all elements using the graphical interface of phpMyAdmin, which is much easier than MySQL command-line editor.
  • phpMyAdmin helps us to control the user’s permission and operate several servers at the same time.
  • We can also backup our database and export the data into different formats like XML, CSV, SQL, PDF, OpenDocument Text, Excel, Word, and Spreadsheet, etc.
  • We can execute complex SQL statements and queries, create and edit functions, triggers, and events using the graphical interface of phpMyAdmin.

Disadvantage of phpMyAdmin

  • phpMyAdmin is a simple interface, but quite tough for a beginner to learn.
  • phpMyAdmin is difficult to install as it needs three more software tools before installation, which is- Apache server, PHP, and MySQL.
  • We have to install all these software tools individually, whereas XAMPP already contains them in a single package. XAMPP is the easiest way to get phpMyAdmin.
  • It has no schema visualization.
  • phpMyAdmin is a web-based software tool which runs only on the browser, so It completely depends on browsers.
  • It does not have auto-compilation capability.

Data Backup problem with phpMyAdmin

phpMyAdmin lacks a lot of features in import/export functionality. There are some backup problems with phpMyAdmin that are given below:

  • Scheduling – There is no way to export the data of the database in phpMyAdmin automatically.
  • Storage media support – As we have discussed earlier, phpMyAdmin is web-based software, so it runs only on the browser. We can take backups only to local drives of our system.
  • Compression, Encryption, and other option – The files which are exported with phpMyAdmin are saved as common text files, with any additional processing. Whereas storing these files in the original form usually takes a lot of disk storage.

Prerequisite

  • Web server – Apache, Nginx, IIS
  • PHP
  • Database – MySQL, MariaDB
  • Web Browser

Web server – phpMyAdmin’s interface is based on our web browser, we need a web server to keep phpMyAdmin’s files inside it. Apache and IIS are popular web servers. We can download Apache web server from here http://mirrors.estointernet.in/apache//httpd/.

PHP – We also need to install PHP 5.3 or upper version to support different functionalities. It contains different extensions to provide support for these functionalities. For example –

  • Session support -SPL (Standard PHP Library) extension
  • Uploading of ZIP files support -PHP zip extension
  • Cookie authentication – mcrypt extension
  • Open Document Spreadsheet and XML importing support –libxml extension

We can download PHP from here. https://www.php.net/downloads.php.

Database – phpMyAdmin supports databases, i.e.

  • MySQL 5.5 or latest versions
  • MariaDB 5.5 or latest versions

Download the MySQL database from here https://dev.mysql.com/downloads/file/?id=486088 or MariaDB database from here https://mariadb.org/download/.

Web Browser – Web browser is required to access phpMyAdmin with enabled cookie and JavaScript. It can be Chrome, Internet Explorer, etc.

Difference between phpMyAdmin and MySQL Console

  1. phpMyAdmin provides a user interface through which we can execute query within SQL. We can also paste the query into SQL to test our output, whereas on MySQL Console we cannot copy and paste queries. We have to write query every time to execute on MySQL console.
  2. MySQL is a database server, on the other hand, phpMyAdmin is a server tool to access the database on MySQL server.
  3. We have to learn database query to create, delete, alter, update, and drop to execute on MySQL console, whereas in phpMyAdmin we can do it using graphical user interface which automatically executes these queries in background.

Note: In simple words, MySQL is a database, and phpMyAdmin is a software tool to access data from that database.

How to install phpMyAdmin?

Here we are going to discuss that how to download phpMyAdmin on Windows operating system. Below are the steps-

Step 1:

Download the latest version of phpMyAdmin software tool from here https://www.phpmyadmin.net/ as per the following instruction. Click on the download button to start downloading.

phpMyAdmin

Step 2:

A popup window will open. Click on Close button and move to the next step.

phpMyAdmin

Step 3:

Extract the downloaded file by right-clicking on the file and select Extract here, and rename the folder name with name phpmyadmin for easy access on the browser.

phpMyAdmin

Step 4:

Once all the files of phpMyAdmin extracted successfully, move the extracted folder of phpMyAdmin from downloaded location to C:\Apache\htdocs. We can also extract files directly in the htdocs folder of apache.

phpMyAdmin

Step 5:

Go inside the conf folder of apache in c drive and open httpd file.

phpMyAdmin

Step 6:

Press Ctrl+F key and find index.html written in the file. Now, replace .html extension with .php and save the file.

phpMyAdmin
phpMyAdmin

Step 7:

Now check the status that apache server is running or not from the services of your computer system? If it is not running, then we need to run the apache server to open phpMyAdmin interface on the browser.

phpMyAdmin

Step 8:

Now open the browser and type http://localhost/phpmyadmin/. phpMyAdmin will start running in the browser.

phpMyAdmin

How to work with phpMyAdmin?

Click on New (1) to create a database and enter the database name in Create database (2) field and then click on Create (3) button. We can create any number of databases.

phpMyAdmin

Enter the table name, number of columns, and click on Go. A message will show that the table is created successfully.

phpMyAdmin

Now enter the field name, type, their size, and any constraint here and save it.

phpMyAdmin

The table is created successfully. We can make changes in the table from here.

phpMyAdmin

Database creation using coding with phpMyAdmin

We can create the database and tables using phpMyAdmin graphical user interface as well as with coding in php.

<?php  
    $servername = "localhost";  
    $username = "root";  //default user name is root  
    $password = "";     //default password is blank  
    $conn = mysqli_connect($servername, $username, $password);  
    if(!$conn)  
        die("Connection failed".mysqli_connect_error());  
    else      
        //echo "Successfully connected with database";  
    $query = "CREATE DATABASE newDB";  
    if (mysqli_query($conn, $query)) {  
        echo "Database created successfully with the name newDB";  
    } else {  
        echo "Error creating database: " . mysqli_error($conn);  
    }  
    mysqli_close($conn);  
?>  

Output:

Database created successfully with the name newDB.

phpMyAdmin

Database connectivity with phpMyAdmin

In phpMyAdmin, we create a database using a graphical user interface as well as by running queries.

<?php  
    $dbhost="localhost";  
    $dbName="newDB";  
    $user="root";  
    $pass="";  
    $conn = new mysqli("mysql:host=$dbhost;dbname=$dbName",$user,$pass);  
    try{  
        echo "Successfully connected with newdb database";  
    }  
    catch(Exception $e){  
            die("Connection failed".$e->getMessage());  
    }  
?>  

Output:

Successfully connected with the database.

phpMyAdmin

PHP MySQL Select Query

PHP mysql_query() function is used to execute select query. Since PHP 5.5, mysql_query() function is deprecated. Now it is recommended to use one of the 2 alternatives.

  • mysqli_query()
  • PDO::__query()

There are two other MySQLi functions used in select query.

  • mysqli_num_rows(mysqli_result $result): returns number of rows.
  • mysqli_fetch_assoc(mysqli_result $result): returns row as an associative array. Each key of the array represents the column name of the table. It return NULL if there are no more rows.

PHP MySQLi Select Query Example

Example

 

<?php  
$host = 'localhost:3306';  
$user = '';  
$pass = '';  
$dbname = 'test';  
$conn = mysqli_connect($host, $user, $pass,$dbname);  
if(!$conn){  
  die('Could not connect: '.mysqli_connect_error());  
}  
echo 'Connected successfully<br/>';  
  
$sql = 'SELECT * FROM emp4';  
$retval=mysqli_query($conn, $sql);  
  
if(mysqli_num_rows($retval) > 0){  
 while($row = mysqli_fetch_assoc($retval)){  
    echo "EMP ID :{$row['id']}  <br> ".  
         "EMP NAME : {$row['name']} <br> ".  
         "EMP SALARY : {$row['salary']} <br> ".  
         "--------------------------------<br>";  
 } //end of while  
}else{  
echo "0 results";  
}  
mysqli_close($conn);  
?> 

Output:

Connected successfully
EMP ID :1 
EMP NAME : ratan 
EMP SALARY : 9000 
--------------------------------
EMP ID :2 
EMP NAME : karan 
EMP SALARY : 40000 
--------------------------------
EMP ID :3 
EMP NAME : jai 
EMP SALARY : 90000 
--------------------------------

PHP MySQL Create Table

PHP mysql_query() function is used to create table. Since PHP 5.5, mysql_query() function is deprecated. Now it is recommended to use one of the 2 alternatives.

  • mysqli_query()
  • PDO::__query()

PHP MySQLi Create Table Example

Example

<?php  
$host = 'localhost:3306';  
$user = '';  
$pass = '';  
$dbname = 'test';  
  
$conn = mysqli_connect($host, $user, $pass,$dbname);  
if(!$conn){  
  die('Could not connect: '.mysqli_connect_error());  
}  
echo 'Connected successfully<br/>';  
  
$sql = "create table emp5(id INT AUTO_INCREMENT,name VARCHAR(20) NOT NULL,  
emp_salary INT NOT NULL,primary key (id))";  
if(mysqli_query($conn, $sql)){  
 echo "Table emp5 created successfully";  
}else{  
echo "Could not create table: ". mysqli_error($conn);  
}  
  
mysqli_close($conn);  
?>  

Output:

Connected successfully
Table emp5 created successfully

PHP MySQL Create Database

Since PHP 4.3, mysql_create_db() function is deprecated. Now it is recommended to use one of the 2 alternatives.

  • mysqli_query()
  • PDO::__query()

PHP MySQLi Create Database Example

Example

<?php  
$host = 'localhost:3306';  
$user = '';  
$pass = '';  
$conn = mysqli_connect($host, $user, $pass);  
if(! $conn )  
{  
  die('Could not connect: ' . mysqli_connect_error());  
}  
echo 'Connected successfully<br/>';  
  
$sql = 'CREATE Database mydb';  
if(mysqli_query( $conn,$sql)){  
  echo "Database mydb created successfully.";  
}else{  
echo "Sorry, database creation failed ".mysqli_error($conn);  
}  
mysqli_close($conn);  
?>  

Output:

Connected successfully
Database mydb created successfully.

PHP MySQL Connect

Since PHP 5.5, mysql_connect() extension is deprecated. Now it is recommended to use one of the 2 alternatives.

  • mysqli_connect()
  • PDO::__construct()

PHP mysqli_connect()

PHP mysqli_connect() function is used to connect with MySQL database. It returns resource if connection is established or null.Syntax

  1. resource mysqli_connect (server, username, password)  

PHP mysqli_close()

PHP mysqli_close() function is used to disconnect with MySQL database. It returns true if connection is closed or false.

Syntax

  1. bool mysqli_close(resource $resource_link)  

PHP MySQL Connect Example

Example

<?php  
$host = 'localhost:3306';  
$user = '';  
$pass = '';  
$conn = mysqli_connect($host, $user, $pass);  
if(! $conn )  
{  
  die('Could not connect: ' . mysqli_error());  
}  
echo 'Connected successfully';  
mysqli_close($conn);  
?>  

Output:

Connected successfully

PHP Open File

PHP fopen() function is used to open file or URL and returns resource. The fopen() function accepts two arguments: $filename and $mode. The $filename represents the file to be opended and $mode represents the file mode for example read-only, read-write, write-only etc.

Syntax

  1. resource fopen ( string $filename , string $mode [, bool $use_include_path = false [, resource $context ]] )  

PHP Open File Mode

ModeDescription
rOpens file in read-only mode. It places the file pointer at the beginning of the file.
r+Opens file in read-write mode. It places the file pointer at the beginning of the file.
wOpens file in write-only mode. It places the file pointer to the beginning of the file and truncates the file to zero length. If file is not found, it creates a new file.
w+Opens file in read-write mode. It places the file pointer to the beginning of the file and truncates the file to zero length. If file is not found, it creates a new file.
aOpens file in write-only mode. It places the file pointer to the end of the file. If file is not found, it creates a new file.
a+Opens file in read-write mode. It places the file pointer to the end of the file. If file is not found, it creates a new file.
xCreates and opens file in write-only mode. It places the file pointer at the beginning of the file. If file is found, fopen() function returns FALSE.
x+It is same as x but it creates and opens file in read-write mode.
cOpens file in write-only mode. If the file does not exist, it is created. If it exists, it is neither truncated (as opposed to ‘w’), nor the call to this function fails (as is the case with ‘x’). The file pointer is positioned on the beginning of the file
c+It is same as c but it opens file in read-write mode.

PHP Open File Example

<?php  
$handle = fopen("c:\\folder\\file.txt", "r");  
?>  

Install PHP

To install PHP, we will suggest you to install AMP (Apache, MySQL, PHP) software stack. It is available for all operating systems. There are many AMP options available in the market that are given below:

  • WAMP for Windows
  • LAMP for Linux
  • MAMP for Mac
  • SAMP for Solaris
  • FAMP for FreeBSD
  • XAMPP (Cross, Apache, MySQL, PHP, Perl) for Cross Platform: It includes some other components too such as FileZilla, OpenSSL, Webalizer, Mercury Mail, etc.

If you are on Windows and don’t want Perl and other features of XAMPP, you should go for WAMP. In a similar way, you may use LAMP for Linux and MAMP for Macintosh.

Download and Install WAMP Server

Click me to download WAMP server

Download and Install LAMP Server

Click me to download LAMP server

Download and Install MAMP Server

Click me to download MAMP server

Download and Install XAMPP Server

Click me to download XAMPP server

How to install XAMPP server on windows

We will learn how to install the XAMPP server on windows platform step by step. Follow the below steps and install the XAMPP server on your system.

Step 1: Click on the above link provided to download the XAMPP server according to your window requirement.

Install PHP

Step 2: After downloading XAMPP, double click on the downloaded file and allow XAMPP to make changes in your system. A window will pop-up, where you have to click on the Next button.

Install PHP

Step 3: Here, select the components, which you want to install and click Next.

Install PHP

Step 4: Choose a folder where you want to install the XAMPP in your system and click Next.

Install PHP

Step 5: Click Next and move ahead.

Install PHP

Step 6: XAMPP is ready to install, so click on the Next button and install the XAMPP.

Install PHP

Step 7: A finish window will display after successful installation. Click on the Finish button.

Install PHP

Step 8: Choose your preferred language.

Install PHP

Step 9: XAMPP is ready to use. Start the Apache server and MySQL and run the php program on the localhost.

How to run PHP programs on XAMPP, see in the next tutorial.

Install PHP

Step 10: If no error is shown, then XAMPP is running successfully.

Install PHP

Windows Server 2012 R2 Installation Process step by step

In this post, I will show you installation of Windows Server 2012 R2 step by step process. Before, you start a clean installation of Windows Server 2012 R2, we have to review the minimum system requirements for windows server 2012 r2. If system requirement is not meet, you will not able to install windows server 2012 r2.

Minimum System Requirements:

  • CPU: Minimum 1.4 GHz 64-bit processor
  • RAM: Minimum 512 MB
  • Disk:  Minimum 32 GB
  • Network: Gigabit (10/100/1000 Ethernet adapter)

Microsoft Recommended Requirements:

  • CPU :  2 GHz or faster
  • RAM : 2 GB RAM or greater
  • Disk :  40 GB or greater
  • Network: Gigabit (10/100/1000 Ethernet adapter)

First, download the Windows Server 2012 R2 ISO Image and make sure the server is configured to boot from CD/DVD and then mount the ISO Image to CD/DVD and start the installation.

you will receive the windows setup screen and select the language as English (United States) and click Next.1

Click Install now to begin the installation2

You can install Standard Edition or Datacenter Edition.But, here I am selecting Windows Server 2012 R2Datacenter (Server with a GUI) and click Next3

Check the box, I accept the license terms and click Next4

Select Custom: Install Window only (advanced)5

Select Unallocated space to install Windows and click Next6

Windows installation is in progress7

Provide Administrator password and re-enter password, then click Finish8

Its Finalizing the settings9

Click Begin Install to complete installation10

Press Ctrl+Alt+Delete to sign in11

Provide Administrator password and press Enter12

13                        Once installation is completed means, we have to follow the post installation steps. I hope this is informative for you. Thanks for reading my post……! If you feel worth please share it in social media.