Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

why will this code not work? <?php session_start(); if(isset($_SESSION[\'member_

ID: 3735421 • Letter: W

Question

why will this code not work?

<?php

session_start();

if(isset($_SESSION['member_id']))

{

header("Location:index.php");//redirects to the home page

} // ends isset session

//insert the connection script and end the script if the connection fails

require_once '../../../../../inc_connect.php';

//function to store password as a hashed value

function randomString()

{

$string = md5(rand());

return $string;

}

//initially there are no erros

$error = false;

//check if form s submitted

if(isset($_POST['register']))

{

$fname = mysql_real_escape_string($_POST['fname']);

$lname = mysql_real_escape_string($_POST['lname']);

$email = mysql_real_escape_string($_POST['email']);

$password = mysql_real_escape_string($_POST['password']);

$cpassword = mysql_real_escape_string($_POST['cpassword']);

//validate form data

//fname can contain only letters and spaces

if(!preg_match('/^[a-zA-Zs]+$/',$fname))

{

$error = true;

//message to member if error occurs

$fname_error = "Name must contain only letters and spaces";

}

//lname can contain only letters and spaces

if(!preg_match('/^[a-zA-Zs]+$/',$lname))

{

$error = true;

//message to member if error occurs

$lname_error = "Name must contain only letters and spaces";

}

//validate email to valid maine account

if(!preg_match('/^[a-zA-Z.]+@(unit.maine|maine).edu/', $email))

{

$error = true;

//message to member if error occurs

$email_error = "Please enter a valid University account.";

}// ends email

//text striing with at lease one Number and one Character between 6 and 10 characters

if(strlen($password) < 7)

{

$error = true;

$password_error = "Password must be minimum of 7 characters.";

}

if($password != $cpassword)

{

$error = true;

//messare to member if error occurs

$cpassword_error = "Password and Comfirm Password do not match.";

}

if(!$error)

{

//hash and salt the password to make it more secure

$hash = "";

$password = $_POST['password'];

$salt = randomString();

$hash = md5($password.$salt);

$TableName = "members"; //assign the table name to a variable

//insert posted data to the mebers table

$SQLString = "INSERT INTO $TableName " .

"(fname, lname, password_hash, password_salt) VALUES" .

"('$fname', '$lname', '$email', '$hash', '$salt')";

$QueryResults = @mysql_query($SQLString, $DBConnect);

if($QueryResults)

{

//message on successful registration

$successmsg = "Successfully Registered! <a href="login.php">Click here to Login</a>";

}//ends if QR

else

{

//message on uncuccessful registration

echo mysql_error();

$errormsg = "Error in registering... Pleae try again later!.";

}//ends else QR

}//ends if !$error

}//ends isset for register

?>

<!DOCTYPE html>

<html>

<head>

<meta character="UFT-8">

<title>Chinese Zodiac Networking</title>

<meta name="viewport" content=" width=device=width, initial-scale=1">

<link rel="stylesheet" type="text/css" href="css/bootstrap.min.css">

<style type= "text/css">

body, .navbar navbar-default

{

backgroung-color: rgb(235,244,251);

}

</style>

</head>

<body>

<nav class="navbar navbar-default" role="navigation">

<div class="container-fluid">

<div class="navbar-header">

<button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#cz_navbar">

<span class="sr-only">Toggle navigation</span>

<span class="icon-bar"></span>

<span class="icon-bar"></span>

<span class="icon-bar"></span>

</button>

<a class="navbar-brand" href="index.php">Chinese Zodiac Social Networking</a>

</div><!-- ends class navbar-header-->

<div class="collapse navbar-collapse" id="cz-navbar">

<ul class="nav navbar-nav navbar-right">

<li><a href="login.php">Login</a></li>

<li class="active"><a href="register.php">Register</a></li>

</ul><!-- ends class nav bar right-->

</div><!-- ends class navbar collapse-->

</div><!-- class container fluid-->

</nav><!-- ends nav-->

<!-- Begin Registration form -->

<div class= "container">

<div class="row">

<div class="col-md-4 col-md-offet-4">

<form role="form" action="register.php" method="post" name="registration_form">

<fieldset>

<legend>Register</legend>

<div class="form-group">

<label for="fname">First Name</label>

<!--use function to remove html characters -->

<input type="text" class = "form-control" id = "fname" name="fname" placeholder="Enter First Name" required value="<?php echo htmlspecialchars($_POST['fname']);?>">

<!--displays error message if user input is not valid -->

<?php echo "<p class='text-danger'>$fname_error</p>";?>

</div>

<div class="form-group">

<label for="lname">Last Name</label>

<!--use function to remove html characters -->

<input type="text" class = "form-control" id = "lname" name="lname" placeholder="Enter Last Name" required value="<?php echo htmlspecialchars($_POST['lname']);?>">

<!--displays error message if user input is not valid -->

<?php echo "<p class='text-danger'>$lname_error</p>";?>

</div>

<div class="form-group">

<label for="email">Email</label>

<!--use function to remove html characters -->

<input type="text" class = "form-control" id = "email" name="email" placeholder="Enter Email"

required value="<?php echo htmlspecialchars($_POST['email']);?>">

<!--displays error message if user input is not valid -->

<?php echo "<p class='text-danger'>$email_error</p>";?>

</div>

<div class="form-group">

<label for="password">Password</label>

<!--use function to remove html characters -->

<input type="text" class = "form-control" id = "password" name="password" placeholder="Enter Password"

required value="<?php echo htmlspecialchars($_POST['password']);?>">

<!--displays error message if user input is not valid -->

<?php echo "<p class='text-danger'>$password_error</p>";?>

</div>

<div class="form-group">

<label for="cpassword">Comfirm Password</label>

<!--use function to remove html characters -->

<input type="text" class = "form-control" id = "cpassword" name="cpassword" placeholder="Comfirm Password"

required value="<?php echo htmlspecialchars($_POST['cpassword']);?>">

<!--displays error message if user input is not valid -->

<?php echo "<p class='text-danger'>$cpassword_error</p>";?>

</div>

<div class="form-group">

<input type="submit" name="register" value="register" class="btn btn-info" />

</div><!--ends form submit-->

</fieldset><!-- ends fields-->

</form><!--ends from-->

<span class="text-success"><?php if (isset($successmsg)) { echo $successmsg;} ?></span>

<span class="text-danger"><?php if (isset($errormsg)) { echo $errormsg;} ?></span>

</div><!-- ends col-md-4-->

</div><!-- ends row-->

<div class="row">

<div class="col-md-4 col-md-offset-4 text-center">

Already Registered?<a href="login.php"> Login Here</a>

</div><!-- ends center text -->

</div><!-- ends row -->

</div><!--ends form container-->

</body>

</html> <!--Ends Registration from-->

Explanation / Answer

This code is absolutely perfect... may be database connection may be getting wrong... It was included on other php file...

otherwise... you didn't gave filename as register.php....

You must run this script from server like lampp, apache, ngnix, etc...

GIve me comment. Which error that you got...