CS 207: Applied Database Practicum
Week 3
Installing XAMPP, GET and POST methods, sending
mail using PHP, and JSON
Varun Dutt
School of Computing and Electrical Engineering
School of Humanities and Social Sciences
Indian Institute of Technology Mandi, India
Scaling the Heights
1
Step 1
Head to [Link] and download the installer for your
operating system.
2
Step 2
The installer is much the same as for any other software - just double click it to run then
follow the prompts.
3
Step 3
You won’t need to change any of the installation settings, the defaults are all good as is.
4
Step 4
Of particular note, you should leave the installation drive as the C:\ drive in order for things to
function correctly. You may also need to grant approval for XAMPP’s Apache installation to have
network access through Windows Defender or any other firewall you might be running.
5
Step 5
Continue following the prompts. When the installation is complete, leave the option checked that
says “Do you want to run the Control Panel now?”, then close the installer and you’ll see the Control
Panel appear. If you missed the chance to leave that box checked, just run XAMPP from your list of
installed software to start the Control Panel instead.
There are really only two
buttons on this Control Panel
you need to know about, and
they’re the two Start buttons
next to Apache and MySQL.
Click both of those buttons
and you’ll see their labels
change to Running, signifying
your local server is now up
and running, ready for
WordPress to be added to it.
6
Step 6: How to run PHP program in Xampp step-by-
step?
● Write this program in a notepad and save it as [Link] (or any other name).
○ <?php echo "Welcome to CS 207 class" ?>
● After completion of the installation, you can use the XAMPP Control Panel to start/ stop all
servers.
● Start Mysql and Apache servers.
7
Step 6: How to run PHP program in Xampp step-by-
step?
● Create a new folder inside htdocs folder (C:/XAMPP/htdocs) and name the folder with your Project name.
● Copy [Link] to your folder inside htdocs.
● In order to get the dashboard for localhost: search [Link] (or [Link] in any browser.
8
Step 6: How to run PHP program in Xampp step-by-
step?
● Create a new folder inside htdocs folder (C:/XAMPP/htdocs) and name the folder with your Project name.
● Copy [Link] to your folder inside htdocs.
● In order to get the dashboard for localhost: search [Link] (or [Link] in any browser.
9
INTRODUCTION : GET and POST
A client (browser) submits an HTTP request to the server; then the server returns a
response to the client. The response contains status information about the request and
may also contain the requested content.
There are two HTTP request methods: GET and POST
1. GET – Requests data from a specified resource.
2. POST – Submits data to be processed to a specified resource.
10
GET
- In GET method the data is sent as URL
- Parameters are usually strings of name and value pairs separated by
ampersands (&).
- Before sending any information , it converts values/data into a query
string in URL known as Url Encoding. Which contains both page link
and encoded information separated by the ? character.
11
POST
- In POST method the data is sent to the server as a package in a
separate communication with the processing script. Data sent through
POST method will not be visible in the URL.
- It is more secure than GET because user-entered information is never
visible in the URL query string or in the server logs.
12
GET - example1
13 13
POST- example1
14 14
GET - example2
<?php
if( $_GET["name"] || $_GET["age"] ) {
echo "Welcome ". $_GET['name']. "<br />";
echo "You are ". $_GET['age']. " years old.";
exit();
}
?>
<html>
<body>
<form action = "<?php $_PHP_SELF ?>" method = "GET">
Name: <input type = "text" name = "name" />
Age: <input type = "text" name = "age" />
<input type = "submit" />
</form>
</body>
</html>
15
POST- example2
<?php
if( $_POST["name"] || $_POST["age"] ) {
if (preg_match("/[^A-Za-z'-]/",$_POST['name'] )) {
die ("invalid name and name should be alpha");
}
echo "Welcome ". $_POST['name']. "<br />";
echo "You are ". $_POST['age']. " years old.";
exit();
}
?>
<html>
<body>
<form action = "<?php $_PHP_SELF ?>" method = "POST">
Name: <input type = "text" name = "name" />
Age: <input type = "text" name = "age" />
<input type = "submit" />
</form>
</body>
</html>
16
GET - example 3
<html>
<body>
<form action="[Link]" method="get">
Name: <input type="text" name="name">
Email: <input type="text" name="email">
<input type="submit">
</form>
</body>
</html>
17
[Link] looks like this:
<html>
<body>
Welcome <?php echo $_GET["name"]; ?>!
Your email address is <?php echo $_GET["email"]; ?>
</body>
</html>
When the user clicks on the "Submit button", the URL will be something like this:
18
POST- example3
<html>
<body>
<form action="[Link]" method="post">
Name: <input type="text" name="name">
Email: <input type="text" name="email">
<input type="submit">
</form>
</body>
</html>
When the user fills out and submits the form, then form data will be sent to PHP file: called
[Link]
19
[Link] page has following code to print submitted data:
<html>
<body>
Welcome <?php echo $_POST["name"]; ?>!
Your email address is <?php echo $_POST["email"]; ?>
</body>
</html>
When the user clicks on the "Submit button", the URL will be something like this:
20
Email using PHP
The mail function accepts the following parameters:
● Email address
● Subject
● Message
● CC or BCC email addresses
The PHP mail function has the following basic syntax:
HERE,
● “$to_email_address” is the email address of the mail recipient
● “$subject” is the email subject
● “$message” is the message to be sent.
● “[$headers]” is optional, it can be used to include information such as CC, BCC
21
Simple Mail Transmission Protocol (SMTP)
● PHP mailer uses Simple Mail Transmission Protocol (SMTP) to send mail.
● On a hosted server, the SMTP settings would have already been set.
● The SMTP mail settings can be configured from “[Link]” file in the PHP installation folder.
● Configuring SMTP settings on your localhost Assuming you are using xampp on windows, locate the
“[Link]” in the directory “C:\xampp\php”.
● Open it using notepad or any text editor. We will use notepad in this example. Click on the edit menu
22
Simple Mail Transmission Protocol (SMTP)
● Click on Find… menu
● The find dialog menu will appear
● Click on Find Next button
23
Simple Mail Transmission Protocol (SMTP)
● Locate the entries
○ [mail function]
○ ; XAMPP: Don’t remove the semicolon if you want to work with an SMTP Server
○ ; SMTP = localhost
○ ; smtp_port = 25
○ Remove the semi colons before SMTP and smtp_port and set the SMTP to your smtp server and the
port to your smtp port. Your settings should look as follows
■ SMTP = [Link]
■ smtp_port = 25
■ Note the SMTP settings can be gotten from your web hosting providers.
■ If the server requires authentication, then add the following lines.
● auth_username = example_username@[Link]
● auth_password = example_password
● Save the new changes.
● Restart Apache server.
24
PHP Mail Example
● Let’s now look at an example that sends a simple mail.
● Output:
25
PHP and JSON
JSON stands for JavaScript Object Notation, and is a syntax for storing and exchanging data.
Since the JSON format is a text-based format, it can easily be sent to and from a server, and used as a
data format by any programming language.
PHP has some built-in functions to handle JSON. First, we will look at the following two functions:
● json_encode()
● json_decode()
PHP - json_encode():
The json_encode() function is used to encode a value to JSON format.
OUTPUT:
{"Peter":35,"Ben":37,"Joe":43}
26
PHP and JSON
PHP - json_decode():
The json_decode() function is used to decode a JSON object into a PHP object or an associative array.
● Example below shows how to access the values from a PHP object:
OUTPUT: 353743
● Example below shows how to access the values from a PHP associative array:
OUTPUT: 353743
27
Thank you
28