In: Computer Science
PHP Please read carefully - display the USER info for IP address, web browser and operating system - do not hard code these parts
1. Create a new PHP document (nit Objective 1)
2. Write a comment similar to the following: "This is my first PHP document, which displays some data in the web browser"(Unit Objective 1)
3. Assign your name as a string value into the variable labeled myName (Unit Objective 2)
4. Assign 53870 numeric value into the variable named randomNumber (Unit Objective 2)
5. ssign the name of the web browser and operating system of the user accessing the file into the variable named userAgent (Unit Objective 2)
6. Assign the file name of the currently running script into the variable named fileName (Unit Objective 3)
7. Assign the IP address from which the user is viewing the current page into the variable named ipAddress (Unit Objective 3)
8. Use echo command in order to display some informative descriptions followed by the values of myNem, randomNumber, userAgent, fileName, and ipAddress variables in the web browser. Each informative description and the value pair whould be displayed on a separate line (Unit Objective 4)
9. Save the file as myFirstPHP.php
10. Create a folder within the public_html folder in your account in people.ysu.edu, name this folder as "e_commerce".
11. Create another folder within the "e_commerce" folder you just created, and name this folder as "Assignment_1", and upload "myFirstPHP.php" file into the "Assignment_1" folder.
12. Attach "myFirstPHP.php" file to your response to this assignment, and submit it.
Below is the solution:
myFirstPHP.php:
<?php
echo "This is my first PHP document, which displays some data in the web browser.</br>";
//Unit Objective 1
$myName = "John Doe"; //assing name to the variable
$randomNumber = 53870; //assign Numeric value
echo "your name is:".$myName."</br>";
echo "Numeric number is : ".$randomNumber."</br>";
// echo $myName; //
$userAgent = get_current_user(); //assign current user name
echo "Use Agent is: ".$userAgent."</br>";
//Unit Objective 2
function get_browser_name($user_agent) //get the browser info
{
//check for the each browser like Opera,Edge,Chrome,Safari,Firefox,MSIE etc
if (strpos($user_agent, 'Opera') || strpos($user_agent, 'OPR/')) return 'Opera';
elseif (strpos($user_agent, 'Edge')) return 'Edge';
elseif (strpos($user_agent, 'Chrome')) return 'Chrome';
elseif (strpos($user_agent, 'Safari')) return 'Safari';
elseif (strpos($user_agent, 'Firefox')) return 'Firefox';
elseif (strpos($user_agent, 'MSIE') || strpos($user_agent, 'Trident/7')) return 'Internet Explorer';
return 'Other';
}
$os = PHP_OS; //assign OS name to the variable
echo "Browser name is :".get_browser_name($_SERVER['HTTP_USER_AGENT'])."</br>"; //print the browser info
echo "Os name is : ".$os."</br>"; //print Os name
// Unit Objective 3
//get the ip address
$user_ip = $_SERVER['REMOTE_ADDR']; //get the ip address
echo "IP address is : ".$user_ip; // Output IP address [Ex: 177.87.193.134]
?>
sample output:
This is my first PHP document, which displays some data in the
web browser.
your name is:John Doe
Numeric number is : 53870
Use Agent is: JohnPC
Browser name is :Edge
Os name is : WINNT
IP address is : :160.45.30.20