practical - Week 7

2022-08-29

Practical 1: Create and test cookies for the following ?

<?php
1 $string_name = "testcookie";
2 $string_value = "This is a test cookie";
3 $expiry_info = info()+259200;
4 $string_domain = "localhost.localdomain";
?>

Practical 2: Demonstrate the use of cookies at every login for additional security ? Complete the code below ?

<?php
session_start(); 
$secret_word = 'medicaps university'; 
//create validate function here
?>

<?php
unset($username); 
if ($_COOKIE['login']) 
{ 
    list($c_username,$cookie_hash) = preg_split(',',$_COOKIE['login']); 
    if (md5($c_username.$secret_word) == $cookie_hash) 
    { 
        $username = $c_username;
    } 
    else 
    { 
        print "You have sent a bad cookie."; 
    } 
} 
if ($username)
{ 
  print "Welcome, $username."; 
} 
else 
{ 
  print "Welcome, anonymous user."; 
} 
?>

<?php
if (isset($_SESSION['loggedInUser'])) {
?>
   print('logged in HTML and code here');
<?php
} 
else 
{
if(isset($_POST['submit'])) {
  if (validate($_REQUEST['username'],$_REQUEST['password'])) 
  { 
      $cookie_name = "login";
      $cookie_value = $_REQUEST['username'].','.md5($_REQUEST['username'].$secret_word);
      setcookie($cookie_name,$cookie_value, time() + 1 * 24 * 60 * 60);
      $_SESSION['loggedInUser'] = $_REQUEST['username'];
  } else {
    print('<br>error invalid user name or password');
  }
} else {
  print('<br>html form here');
}
}
?>

Practical 3: Write a php program to Count Visits with session. Complete the code below ?