<?php
//TASK b - create variables named usr, psw, pswErr, and usrErr and initialize them to empty values

$logErr = true;
$display = "display:none;";
$usr = "";
$psw = "";
$pswErr = "";
$usrErr = "";

//TASK c - replace "aaaa" with the proper superglobal variable to capture all the post array elements
if ($_POST == "POST") {

  $display = "display:block;";

//TASK d - replace "bbbb" with the proper superglobal variable to capture the username field
  $usr = validate_fields($_POST["usr"]);
  $pattern = "/^[a-zA-Z ]*$/";
  if (empty($usr)) {
    $usrErr = "Username is required";
    $logErr = true;
  } elseif (!preg_match($pattern,$usr)) {
     $usrErr = "Only letters allowed"; 
     $logErr = true;
  } else {
     $logErr = false;
  }
 
 
//Task e - replace "cccc" with the proper superglobal variable to capture the password field
  $psw = validate_fields($_POST["psw"]);
//TASK f - using the pattern above, create a new pattern to validate the password field 
  $pattern = "(?!.* )(?=.*[\W_])(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{16,}";    
  if (empty($psw)) {
    $pswErr = "Password is required";
    $logErr = true;
  } elseif (!preg_match($pattern,$psw)) {
     $pswErr = "Invalid Password"; 
     $logErr = true;
  } else {
    $logErr = false; 
  }    
}

if ( !$logErr)  {
//TASK g - Issue a Session ID
session_start();    
echo session_id();
}

//TASK h - create a function called validate_fields() that accepts the data field checks for new lines, leading and trailing spaces, removes backslashes, and encodes HTML and return the cleaned data back
function validate_fields($data) {
    return htmlentities(trim(str_replace(array("\n", "\r", "\\"), '', $data)));
}
?>

<!DOCTYPE html>
<html lang="en">
<head>
   <title>Login Form</title>
   <meta charset="utf-8">
    <style>
        /* Style body and text */
body {
	background-color: #3598dc;
	font-family: arial;
}

.text-center {
    text-align: center;
}
   
/* Style all input fields */
input {
  width: 100%;
  padding: 12px;
  border: 1px solid #ccc;
  border-radius: 4px;
  box-sizing: border-box;
  margin-top: 6px;
  margin-bottom: 16px;
  background-color: #f2f2f2;
}

/* Style the submit button */
#sub {
  background-color: #007bff;
  color: white;
}
#sub:hover {
  background-color: #0062cc;
}

/* Style the container for inputs */
.container {
  width: 300px;
  margin: 0 auto;
  background-color: #ffffff;
  padding: 20px;
}

/* The message box is shown only when the user clicks on the password field */
#message {
  display:none;
  width: 300px;
  margin: 0 auto;
  background: #f1f1f1;
  color: #000;
  position: relative;
  padding: 20px;
  margin-top: 10px;
}

#message p {
  padding: 10px 35px;
  font-size: 16px;
}

/* Add a green text color when the requirements are right */
.valid {
  color: #009b6c;
}

/* Add a red text color when the requirements are wrong */
.invalid {
  color: #d9002f;
}
    </style>
</head>
<body>    
   <div class="container">
      <form name="loginform" id="loginform" method="post" action="<?php $_SERVER['PHP_SELF'] ?>">
         <p>Username<br>
         <input type="text" id="usr" name="usr"></p>
	     <p>Password<br>
         <input type="password" id="psw" name="psw">
	     </p>
         <input type="submit" id="sub" name="sub" value="Log In">
      </form>
   </div>
   <div id="message" style="<?php echo $display; ?>">
     <?php if (!empty($usrErr)) { ?>
     <p class="invalid"><?php echo $usrErr; ?></p>
     <?php } ?>
     <?php if (!empty($pswErr)) { ?>
     <p class="invalid"><?php echo $pswErr; ?></p>
     <?php } ?>
    
     <p>Username: <?php echo $usr; ?></p>
     <p>Password: <?php echo $psw; ?></p>
     <p>Session ID: <?php echo session_id(); ?></p>  
   </div>
</body>
</html> 

PHP Online Compiler

Write, Run & Share PHP code online using OneCompiler's PHP online compiler for free. It's one of the robust, feature-rich online compilers for PHP language, running on the latest version 7. Getting started with the OneCompiler's PHP compiler is simple and pretty fast. The editor shows sample boilerplate code when you choose language as PHP and start coding.

Taking inputs (stdin)

OneCompiler's PHP online editor supports stdin and users can give inputs to programs using the STDIN textbox under the I/O tab. Following is a sample PHP program which takes name as input and prints hello message with your name.

<?php
	fscanf(STDIN, "%s\n", $name);           
    echo "Hello ".$name.".\n";
?>

About PHP

PHP(Hypertext Preprocessor) is widely used server sripting language by Rasmus Lerdorf in the year 1994.

Key features

  • Free
  • powerful tool for making dynamic and interactive web pages
  • can integrate with almost all popular databases like MySQL, PostgreSQL, Oracle, Sybase, Informix, Microsoft SQL Server etc.
  • C like Syntax and easy to learn.
  • Object oriented scripting language.
  • easily embeddable into HTML
  • Loosely typed language.

Syntax help

Variables

In PHP, there is no need to explicitly declare variables to reserve memory space. When you assign a value to a variable, declaration happens automatically. Variables are case-sensitive in PHP.

$variable_name = value;  

Loops

1. IF Family:

If, If-else, Nested-Ifs are used when you want to perform a certain set of operations based on conditional expressions.

If

if(conditional-expression){    
//code    
} 

If-else

if(conditional-expression){  
//code if condition is true  
} else {  
//code if condition is false  
} 

Nested-If-else

if(condition-expression1) {  
    //code if above condition is true  
} elseif(condition-expression2){  
    //code if above condition is true  
}  
elseif(condition-expression3) {  
    //code if above condition is true  
}  
...  
else {  
    //code if all the conditions are false  
}  

2. Switch:

Switch is used to execute one set of statement from multiple conditions.

switch(conditional-expression) {    
case value1:    
 // code if the above value is matched    
 break;  // optional  
case value2:    
 // code if the above value is matched    
 break;  // optional  
...    
    
default:     
 // code to be executed when all the above cases are not matched;    
} 
 

3. For:

For loop is used to iterate a set of statements based on a condition.

for(Initialization; Condition; Increment/decrement){  
  // code  
} 

For-each:

// you can use any of the below syntax
foreach ($array as $element-value) {  
    //code  
}

foreach ($array as $key => $element-value) {   
    //code 
} 

4. While:

While is also used to iterate a set of statements based on a condition. Usually while is preferred when number of iterations are not known in advance.

while(condition) {  
 // code 
}  

5. Do-While:

Do-while is also used to iterate a set of statements based on a condition. It is mostly used when you need to execute the statements atleast once.

do {
  // code 
} while (condition); 

Functions

Function is a sub-routine which contains set of statements. Usually functions are written when multiple calls are required to same set of statements which increases re-usuability and modularity.

How to define a Function

function function_name(parameters) {  
  //code
}

How to call a Function

function_name (parameters)