Jump to content

use of die function


lanceox

Recommended Posts

Hi there,

 

i have a login page that does checks for things like incorrect passwords, etc however as i cant use die function as it terminates the rest of my code i used header(location).... to send the user back.

 

is there anyway i could get the password checks to work without terminating my code with die()????

 

Thanks

 

Lance

Link to comment
Share on other sites

Using die() to handle user input errors is rarely a good way to do it. If you're validating, you should be able to repopulate and re-present the form with the user's input in the fields, and display an error message detailing the validation errors.

Link to comment
Share on other sites

ahh right okay, tried using die() as i couldnt think of a way around the problem but the user to be redirected to the previous page if login details are incorrect but display a message.

 

Whenever i redirect the message wont display.

This is the code if any needs to see

 


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<title>Forensics E-learning Package</title>
    <script type="text/javascript" src="start.js"></script>

<link rel="stylesheet" type="text/css" href="style.css" />
</head>
<body>
<div id="wrapper">
<div id="header">
<div id="toplinks">
    

</div>
  </div>
<div id="menu">
	<ul>
		<li><a class="selected" href="index.html">Home</a></li>
		<li><a href="index.php">Initial Quiz</a></li>
		<li><a href="about.php">About</a></li>
            <li><a href="member.php">Member Section</a></li>
      

	</ul>
  </div>
<div id="content">
	<div id="main">

<h1>Forensics E-Learning Package</h1><BR /></head>
            Login to the User Profiled E-Learning Course which is specifically aimed to raise awareness in computer forensics.  
<BR /><BR />
<form action='login.php' method='POST'>
        Username:
          <input type='text' name='username'><BR />
        Password: 
         <input type='password' name='password'><BR />
        <input type='submit' value='Log In'>
    </form> <p><BR /><BR />
    <a class="button" href='register.php'><span><button class="button" id="save">Register</button></span></a>

	</div>
	<div id="right">
	<h2>Right Menu</h2>
	<div class="rightitem">
		<ul>
		<li><a class="selected" href="index.html">Home</a></li>
		<li><a href="index.php">Initial Quiz</a></li>
		<li><a href="about.php">About</a></li>
            <li><a href="member.php">Members Area</a></li>
            <li><a href="contact.php">Leave Feedback</a></li>
		</ul>
	</div>
	</div>
  </div>
<div class="clearbottom"></div>
<div id="footer"></div></div>
</body>
</html>

 

page that checks the login information

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<title>Forensics E-learning Package</title>
    

<link rel="stylesheet" type="text/css" href="style.css" />
</head>
<body>
<div id="wrapper">
<div id="header">
<div id="toplinks">

</div>
</div>
<div id="menu">
	<ul>
		<li><a class="selected" href="home.html">Home</a></li>
		<li><a href="initialquiz.php">Initial Quiz</a></li>
		<li><a href="about.php">About</a></li>
            <li><a  href="evidencemodule.php">Evidence Module</a></li>

	</ul>
  </div>
<div id="content">
	<div id="main">
		<h1>Forensics E-Learning Package</h1><BR />
<?php
session_start();

$username = $_POST['username'];
$password = $_POST['password'];

$_SESSION['$username'] = $username;
$_SESSION['$password'] = sha1($password);


if($username && sha1($password))
{
$connect = mysql_connect("localhost","root", "") or die ("Couldn't Connect!");
mysql_select_db("userlogin") or die("Couldn't find db");

//$con = mysql_connect('userscores.db.7767668.hostedresource.com','userscores','L3tt3r09');
//mysql_select_db('userscores', $con);

$query = mysql_query("SELECT * FROM users WHERE username='$username'");


$numrows = mysql_num_rows($query);

if ($numrows!=0)
{
	//code to login
	while ($row = mysql_fetch_assoc($query))
	{
		$dbusername = $row['username'];
		$dbpassword = $row['password'];
		$dbscore = $row['score'];
		$dbdclty = $row['dclty'];
		$dbid = $row['id'];
		$dbnewdclty = $row['newdclty'];
	}

	//$scores = $dbscore;

	//$query1 = mysql_query("SELECT * FROM scores");		

	//while ($row = mysql_fetch_assoc($query))
	//{
		//$dbscores = $row['scores'];
		//$dbid = $row['id'];

	//}


//	$num=mysql_numrows($query1);

	if ($username==$dbusername&&sha1($password)==$dbpassword)
	{
		$username==$dbusername;	
	}
	else
	{
		header('Location: index.php');
		echo "<script>alert(\"incorrect password!\");</script>";
	}
}
else
{
	header('Location: index.php');
	echo("That user doesn't exist!");

}

}
else
{
header('Location: index.php');
echo("Please enter a username and password!");
}

$_SESSION['id'] = $dbid;
$_SESSION['PreviousScore'] = $dbscore;
$_SESSION['dclty'] = $dbdclty;
$_SESSION['newdclty'] = $dbnewdclty;
?>

            
		<h2>Welcome to the Computer Forensics e-Learning Package</h2><BR /><BR />
            
            Hi <b><?php echo $username; ?></b>
            <BR /><BR />
            
	    <p><b>Traditional E-Learning Packages</b><BR />
	    <BR />
	  </p>
	  <p> Traditional e-learning systems are typically informative systems and generally just present information to the user from page to page, which is followed by a final quiz, which tests your knowledge on the subject topic being learned.   The main issues with traditional e-learning systems is that the information presented to the user is non-personalized and there is a general assumption that every learner is on the same page in terms of knowledge level. <br />
		  <br />
              <b>The User Personalized Learning Package</b><BR /><BR />
		  User Personalization over comes issues with traditional e-learning systems through the use of user profiles, for example, rather than viewing information from slide to slide, the user must first take a quiz before hand.  This will allow the user profiling component to calculate the score from the quiz and present the user with relevant learning material based upon the users results. <br />
          <p>          
          <p>Please take the test to receive personalized e-learning content by selecting a difficulty. <BR /><BR />
          
          <table>
          	<tr>
          		<td>
          
  <form name="difficulty selection" method="POST" action="initialquiz.php">
  <input type="radio" name="dclty" id="dcltya" value="1">Beginner<BR>
  <input type="radio" name="dclty" id="dcltyb" value="2">Intermediate<BR>
  <input type="radio" name="dclty" id="dcltyc" value="3">Expert<BR>
  <input type="submit" name="submitdclty" id="button" value="select a difficulty rating!" />
          </form>
          		<td>
          	<tr>
          </table>
          
          <BR />         

        </div>
	<div id="right">
	<h2>Right Menu</h2>
	<div class="rightitem">
		<ul>
		<li><a class="selected" href="home.html">Home</a></li>
		<li><a href="initialquiz.php">Initial Quiz</a></li>
		<li><a href="about.php">About</a>			  </li>
            <li><a href="member.php">Members Area</a>	  </li>
            <li><a href="evidencemodule.php">Evidence Module</a></li>
            <li><a href="contact.php">Leave Feedback</a></li>
            <li><a href="logout.php">Logout</a>			  </li>
		</ul>
		<p> </p>
        </div><em></em>
	</div>
  </div>
<div class="clearbottom"></div>
<div id="footer">
	<p id="legal"> </p>
</div>
</div>
</div>
</body>
</html>

 

Any help would be great.  really stuck on this

 

Thanks

 

Lance

Link to comment
Share on other sites

Thanks for quick replies.

 

I changed the echo so its before, however its still the same.  I tried meta tags and i dont really want to use meta as this will show the message to the user however when they login it will also show member content for example.

 

any more ideas will be great :)

 

 

 

Link to comment
Share on other sites

For a relatively small, simple form like that, I usually handle the processing in the same script, or include() the form within the processing script if the form has been submitted. The way you have it set up now, there's no way a header() will work to send the user back to the form, since the output starts long before header() is called.

 

The logic should look like:

if( form has been submitted ) {
     validate form
     if( validation ok ) {
          run db query
          redirect to "you're now logged in" page if db query returns valid credentials
     } else {
          set flag to include/echo form and errors allow correction and resubmission.
     }
} else {
     set flag to include/echo form
}
if( flag === TRUE ) {
     include 'form'
} 

 

Doing it in that manner, you have nothing output to the browser until the form has been validated, which will allow you to use header() redirects (if needed) in the processing logic, and eliminates the need to store errors in a $_SESSION var when you redirect back to a form.

Link to comment
Share on other sites

If you have problems/questions, just say so. One tip I'll give you up front is to store any validation errors in an array. You can then check to see if the array is empty to determine if the form validated. If it isn't empty, you can implode() the array to display the errors.

Link to comment
Share on other sites

I changed some things around and built it all in one which is similar to the register page but i cant see where the problem is, 

it is either not pulling information from database or assigning it to variables correctly

 


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<title>Forensics E-learning Package</title>
    <script type="text/javascript" src="start.js"></script>

<link rel="stylesheet" type="text/css" href="style.css" />
</head>
<body>
<div id="wrapper">
<div id="header">
<div id="toplinks">
    

</div>
  </div>
<div id="menu">
	<ul>
		<li><a class="selected" href="index.html">Home</a></li>
		<li><a href="index.php">Initial Quiz</a></li>
		<li><a href="about.php">About</a></li>
            <li><a href="member.php">Member Section</a></li>
      

	</ul>
  </div>
<div id="content">
	<div id="main">

<h1>Forensics E-Learning Package</h1><BR /></head>
            Login to the User Profiled E-Learning Course which is specifically aimed to raise awareness in computer forensics.  
<BR /><BR />

<form action='index.php' method='POST'>
        Username:
          <input type='text' name='username'><BR />
        Password: 
         <input type='password' name='password'><BR />
        <input type='submit' value='Log In'>
    </form> <p><BR /><BR />
    <a class="button" href='register.php'><span><button class="button" id="save">Register</button></span></a>
<?php
$submit =&$_POST['submit'];


if($submit)
{
if($username && sha1($password))
{
	$username =&$_POST['username'];
	$password =&$_POST['password'];

	$_SESSION['$username'] = $username;
	$_SESSION['$password'] = sha1($password);

	$connect = mysql_connect("localhost","root", "") or die ("Couldn't Connect!");
	mysql_select_db("userlogin", $connect) or die("Couldn't find db");

	//$con = mysql_connect('userscores.db.7767668.hostedresource.com','userscores','L3tt3r09');
	//mysql_select_db('userscores', $con);

	$query = mysql_query("SELECT * FROM users WHERE username=' $username'");
	$numrows = mysql_num_rows($query);

	if ($numrows!=0)
	{
		//code to login
		while ($row = mysql_fetch_assoc($query))
		{
			$dbusername = $row['username'];
			$dbpassword = $row['password'];
			$dbscore = $row['score'];
			$dbdclty = $row['dclty'];
			$dbid = $row['id'];
			$dbnewdclty = $row['newdclty'];
		}

		$_SESSION['id'] = $dbid;
		$_SESSION['PreviousScore'] = $dbscore;
		$_SESSION['dclty'] = $dbdclty;
		$_SESSION['newdclty'] = $dbnewdclty;



		if ($username==$dbusername&&sha1($password)==$dbpassword)
		{
			$username==$dbusername;	
		}
		else
		{
			echo ("Incorrect Password!");
		}
	}
	else
	{
		echo("That user doesn't exist!");

	}

}
else
{
	echo("Please enter a username and password!");
}
echo ("You Successfully Logged In!");
}
else
{
//echo("Problem Logging In!");
}
?>



	</div>
	<div id="right">
	<h2>Right Menu</h2>
	<div class="rightitem">
		<ul>
		<li><a class="selected" href="index.html">Home</a></li>
		<li><a href="index.php">Initial Quiz</a></li>
		<li><a href="about.php">About</a></li>
            <li><a href="member.php">Members Area</a></li>
            <li><a href="contact.php">Leave Feedback</a></li>
		</ul>
	</div>
	</div>
  </div>
<div class="clearbottom"></div>
<div id="footer"></div></div>
</body>
</html>

 

thanks for all your help so far :)

 

lance

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.