6 Replies - 1119 Views - Last Post: 25 November 2012 - 11:37 AM Rate Topic: -----

#1 Navya10  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 24-November 12

Login Page and error messages

Posted 24 November 2012 - 07:46 PM

Hello,

There is a login page (login.php)which has username, password and submit button, firstly, this page checks a special variable($error_msg)if its empty or not
- if $error_msg is empty,the PHP code does not do anything
- if $error_msg is not empty, the PHP code displays the value of $error_msg
when Login.php is sent to the browser for the first time, it should not display any error message
the form is submitted to Auth_CityForm.php when the user presses the submit button.
Auth_CityForm.php return 2 responses, if the credentials(the username and password are hard-coded values) are invalid, the page "forwards" the response to Login.php, as follows:
1. set the error message variable $error_msg to the value:
"Invalid credentials.
Please enter valid credentials and submit them again."
2. include the Login.php file

If the credentials are valid,the page displays
1. the text "Hello ", followed by the name of the user, as entered by
the user and
2. a form called city_name, which has
1. one text input field, where the user enters a city name
2. one submit button, whose name is SubmitCityName.

The code that I have has errors and I don't know what to do next?
Here's the code for both Login.php and Auth_CityForm.php

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
  <title>Login</title>
 
</head>
<body>
<h1> Login Details </h1>
<?php if (!empty($error_message)) { ?>
      <p class="error"><?php echo $error_message; ?></p>
  <?php } ?>
  <form action="Auth_CityForm.php" method="post">
		<label> Username : </label>
		<input type = "text" name = "username" /> </br>
		<label> Password : </label>
		<input type = "text" name = "password"  /> </br>
		</br>
		
		<input type = "submit" value = "SubmitCredentials"/> </br>
</form>
</body>
</html>



<?php
	$Username = $_POST['username'];
	$Password = $_POST['password'];
	
	//validate username entry
	if (empty($Username))
	{
		$error_msg = "Invalid credentials. Please enter valid credentials and submit them again.";
	}
	else if($Username == '3020ta' AND $Password == '3020')
	{
		echo "Hello, 3020ta";
	}
	else if ($Username == 'asdf' AND $Password == '')
	{
		echo "Hello, asdf";
	}
	// set error message to empty string if no invalid entries
	else
	{
		$error_msg = ' ';
	}
	//if an error message exsts go to login.php 
	if ($error_msg != '')
	{
		include('Login.php');
		exit();
	}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<body>
	<h1> Login Details </h1>
	<label>Username : </label>
	<span> <?php echo Username; ?></span><br/>
	<label>Password : </label>
	<span> <?php echo Password; ?></span><br/>
</body>
</html>



Is This A Good Question/Topic? 0
  • +

Replies To: Login Page and error messages

#2 Dormilich  Icon User is online

  • 痛覚残留
  • member icon

Reputation: 3577
  • View blog
  • Posts: 10,444
  • Joined: 08-June 10

Re: Login Page and error messages

Posted 25 November 2012 - 01:52 AM

View PostNavya10, on 25 November 2012 - 03:46 AM, said:

The code that I have has errors and I don't know what to do next?

post the error messages, otherwise how are we supposed to help?
Was This Post Helpful? 0
  • +
  • -

#3 Navya10  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 24-November 12

Re: Login Page and error messages

Posted 25 November 2012 - 08:48 AM

Sorry about that, I need to display error message : "Invalid credentials.
Please enter valid credentials and submit them again.", when the user enters invalid credentials.
There are two sets of hard coded usernames and passwords that are used to login,
1. (username, password) : (3020ta, 3020)
2. username "asdf" is valid with any password even blank.

when I type in 3020ta for username and 3020 for password and click on theSubmitCredentials button,
its goes to Auth_CityForm.php and the error that is displayed is:

Hello, 3020ta
Notice: Undefined variable: error_msg in C:\xampp\htdocs\example\Auth_CityForm.php on line 24

Login Details (below should be the username and password with text boxes as in the Login page)
Username :
Notice: Use of undefined constant Username - assumed 'Username' in C:\xampp\htdocs\example\Auth_CityForm.php on line 37
Username
Password :
Notice: Use of undefined constant Password - assumed 'Password' in C:\xampp\htdocs\example\Auth_CityForm.php on line 39
Password


Thank You
Was This Post Helpful? 0
  • +
  • -

#4 goku87  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 4
  • View blog
  • Posts: 81
  • Joined: 17-October 09

Re: Login Page and error messages

Posted 25 November 2012 - 09:12 AM

That error simply means you haven't set $error_msg, Username, or Password by those points in the code. Could you post the code from your Auth_CityForm.php file?

This post has been edited by goku87: 25 November 2012 - 09:14 AM

Was This Post Helpful? 0
  • +
  • -

#5 Hqtitan  Icon User is offline

  • D.I.C Head

Reputation: 11
  • View blog
  • Posts: 51
  • Joined: 28-August 10

Re: Login Page and error messages

Posted 25 November 2012 - 09:39 AM

The only time you're setting $errormsg is inside of the if statement at line 8. Since it is only set when $Username is empty, it is undefined in any other circumstances when it is called at line 24.

As for the other two errors, I assume that you're trying to call the variables $Username and $Password. If that is the case, make sure that you use $ in front of the names so that PHP knows that you're referencing a variable.
Was This Post Helpful? 0
  • +
  • -

#6 Navya10  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 24-November 12

Re: Login Page and error messages

Posted 25 November 2012 - 10:57 AM

<?php
	$username = $_POST['username'];
	$password = $_POST['password'];
	
	?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns = "http://www.w3.org/1999/xhtml">
	<head>
	<title> Auth_CityForm </title>
	
	</head>
	
	<body>
	
		<?php 
		
		$userids = array("3020ta", "asdf", "Userid1");
		
		
		$pswd = array("3020", "1000", "2000");
		$error_msg = '';
		
		if ($username != $userids[1] && (empty($username) || empty($password))) {
			$error_msg = 'The fields require username and password';
		}
		else if ($username ==  $userids[0] || $username ==  $userids[1] || $username ==  $userids[2]) {
			if ($username == $userids[0])
			{
				if ($password != $pswd[0]) {
					$error_msg = 'Invalid Credentials, please use valid username or password.';
				}
			}
			else if ($username == $userids[2])
			{
				if ($password != $pswd[2]) {
					$error_msg = 'Invalid Credentials, please use valid username or password.';
				
				}
			}
			else
			{
				$error_msg = '';		
			}
		}
		else {
			$error_msg = 'Username not valid';
		}
		
		if($error_msg != '') {
			include('Login.php');
			exit();	}		
		
		echo "Hello ". $username, "<br />"; ?>
		  <br />
		
		<form action="CityTemperatureResp.php" method="post" 
		name = "city_name">
		<input type="text" name="city" /> &nbsp
		<input type="submit" value="SubmitCityName" name="submitcitybutton" /> <br />
		
		</form>	
	</body>
</html>




I corrected the errors , but I have another question,
if the credentials are invalid( if the username and password are incorrect), the page
"forwards" the response to Login.php, as follows:
1) set the error message variable $error_msg to the value:
"Invalid credentials.
Please enter valid credentials and submit them again."
2) include the Login.php file

Now my problem is, when the username and password are wrong the page should give me the error mesage "Invalid credentials. Please enter valid credentials and submit them again." and should include the Login.php page,
Is my code correct?

Thank You
Was This Post Helpful? 0
  • +
  • -

#7 goku87  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 4
  • View blog
  • Posts: 81
  • Joined: 17-October 09

Re: Login Page and error messages

Posted 25 November 2012 - 11:37 AM

From the looks of it, it probably won't work due to the exit() floating about on line 54.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1