You are on page 1of 16

K Mahesh

RCEW, Pasupula (V), Nandikotkur Road,


Near Venkayapalli, KURNOOL
Syllabi
• Introduction to PHP: The problem with other
Technologies ( Servelets and JSP),
• Downloading, installing, configuring PHP,
Programming in a Web environment and the
anatomy of a PHP Page.
• Overview of PHP Data types and Concepts: Variables
and data types, Operators, Expressions and
Statements, Strings, Arrays and Functions.
• PHP Advanced Concepts: Using Cookies, Using HTTP
Headers, Using Sessions, Authenticating users, Using
Environment and Configuration variables, Working
with Date and Time.
Authenticating users
• PHP uses two predefined variables to authenticate a user.
1. $_SERVER['PHP_AUTH_USER']
2. $_SERVER['PHP_AUT_PW']
• These two superglobal variables store the Username and
password values respectively while authenticating is as
simple as comparing the expected username and
password to these variables.
• The isset() function determines whether a variable has
been assigned a value.
boolean isset(mixed var[,mixed var[,......]])
• It returns TRUE if the variable contains a value and FALSE
if it does not.
<?php
if(!isset($_SERVER['PHP_AUTH_USER']) || !isset($_SERVER['PHP_AUTH_PW']))
{
header('www-Authenticate: Basic replays="Authentication"');
header("HTTP/1.1 401 Unauthorized");
}
else
{
echo "User Name is ".$_SERVER['PHP_AUTH_USER']."<br/>";
echo "password is ".$_SERVER['PHP_AUTH_PW'];
}
?>
<?php
if (!isset($_SERVER['PHP_AUTH_USER'])) {
header("WWW-Authenticate: Basic realm=\"Private Area\"");
header("HTTP/1.0 401 Unauthorized");
print "Sorry - you need valid credentials to be granted access!\n";
exit;
} else {
if (($_SERVER['PHP_AUTH_USER'] == 'mahesh') && ($_SERVER['PHP_AUTH_PW'] ==
'abc123')) {
print "Welcome to the private area!";
} else {
header("WWW-Authenticate: Basic realm=\"Private Area\"");
header("HTTP/1.0 401 Unauthorized");
print "Sorry - you need valid credentials to be granted access!\n";
exit;
}
}
?>
Authenticating users with $_POST / $_GET

<?php
$message="";
if(count($_POST)>0) {
if(($_POST['userName']=='mahesh')&&($_POST['password']=='abc123')) {
$message = "You are successfully authenticated!";
} else {
$message = "Invalid Username or Password!";
}
}
?>
Authenticating users with MySQL
<?php
$message="";
if(count($_POST)>0) {
$conn = mysqli_connect("localhost","root","","mahesh");
$result = mysqli_query($conn,"SELECT * FROM users WHERE user_name='" .
$_POST["userName"] . "' and password = '". $_POST["password"]."'");
$count = mysqli_num_rows($result);
if($count==0) {
$message = "Invalid Username or Password!";
} else {
$message = "You are successfully authenticated!";
}
}
?>
<html> <head>
Html file
<title>User Login</title>
<link rel="stylesheet" type="text/css" href="styles.css" />
</head>
<body>
<form name="frmUser" method="post" action="">
<div class="message"><?php if($message!="") { echo $message; } ?></div>
<table border="0" cellpadding="10" cellspacing="1" width="500" align="center"
class="tblLogin">
<tr class="tableheader">
<td align="center" colspan="2">Enter Login Details</td>
</tr>
<tr class="tablerow">
<td>
<input type="text" name="userName" placeholder="User Name" class="login-input"></td>
</tr>
<tr class="tablerow">
<td>
<input type="password" name="password" placeholder="Password" class="login-input"></td>
</tr>
<tr class="tableheader">
<td align="center" colspan="2"><input type="submit" name="submit" value="Submit"
class="btnSubmit"></td>
</tr>
</table>
</form></body></html>
body{
CSS file
font-family: calibri;
}
.tblLogin {
border: #95bee6 1px solid;
background: #d1e8ff;
border-radius: 4px;
}
.tableheader {
font-size: 24px;
}
.tableheader td {
padding: 20px;
}
.tablerow td{
text-align:center;
}
.message {
color: #FF0000;
font-weight: bold;
text-align: center;
width: 100%;
}
.login-input {
border: #CCC 1px solid;
padding: 10px 20px;
}
.btnSubmit {
padding: 10px 20px;
background: #2c7ac5;
border: #d1e8ff 1px solid;
color: #FFF;
}
Environment & Configuration variables
• PHP provides a means to use and verify the configuration settings
and environment variables relative to the server space the script is
occupying.
• A common use of the environment variables in PHP is for dynamic
imaging. While Windows systems commonly store their fonts in one
folder, Linux-based systems keep theirs in another. By using PHPs
environment variables to determine the current operating system,
you can make your code slightly more portable.
• Using configuration variables can also come in quite handy,
particularly with file upload scripts. The base PHP installation leaves
only enough processing time to upload files that are generally 2MB
or smaller in size. By manipulating the PHP configuration files
temporarily, you can increase the limit enough to allow a script to
process much larger files.
Read Environment variables
• The $_ENV superglobal is PHPs method for reading a systems
environment variables and has an argument set that is based upon
the current environment that is available to it.
• getenv() is a PHP function used for returning the specific
environment variable's value
• putenv() is a PHP function that is used for setting the value of a
particular environment variable

<?php
echo $_ENV['Program Files'] . "<br />"; //Outputs C:\Program Files.
echo getenv("ProgramFiles") . "<br />"; //Outputs C:\Program Files.
echo $_ENV['COMPUTERNAME'] . "<br />"; //Outputs COMPUTER-2339.
echo getenv("COMPUTERNAME") . "<br />"; //Also Outputs COMPUTER-2339.
?>
<?PHP getenv("REMOTE_ADDR"); //fetch the current user's IP address
putenv("tmp=usr"); //set the user's value for environment variable
getenv("tmp");
?>
Setting Environment variables
• Setting environment and configuration variables is just as easy as it
is to get them.
• While working with environment variables, you merely need to
assign a new value to the $_ENV superglobal to process a
temporary change. The change will be in effect for the scripts
duration.

<?php
echo $_ENV['COMPUTERNAME'] . "<br />"; // Echoes COMPUTER-2339.
$_ENV['COMPUTERNAME'] = "dp";
echo $_ENV['COMPUTERNAME'] . "<br />"; //Echoes the new COMPUTERNAME.
?>
Read Configuration variables
• Reading configuration variables, on the other hand, takes place
through two functions, ini_get() and ini_get_all().
• The function ini_get() will retrieve the value of a specified
configuration variable, and
• the function ini_get_all() will retrieve an array filled with the entire
selection of configuration variables that are available.

<?php
echo ini_get ("post_max_size") . "<br />"; //Outputs 8MB.
//And you can output the entire listing with this function.
print_r (ini_get_all());
?>
Setting Configuration variables
• The same applies for configuration variables but with a different
approach.
• To set a configuration variable, you have to use the PHP function
ini_set(), which will allow you to set a configuration variable for the
scripts duration. Once the script finishes executing, the
configuration variable will return to its original state.
string ini_set ( string varname, string newvalue )

<?php
echo ini_get ('post_max_size'); //Echoes 8MB.
//Then you set it to 200M for the duration of the script.
ini_set('post_max_size','200M');
echo ini_get ('post_max_size'); //Echoes 200MB.
//Any files that are to be uploaded in this script will be OK up to 200M.
?>
END of UNIT-3

You might also like