Page 1 of 1
Can we use Echo after a die function?
Posted: Tue Dec 14, 2010 2:35 am
by jagannathg
Hello All,
Needed know can we use echo after a die function or its against the PHP Syntax?
Regards,
Jagannath
Re: Can we use Echo after a die function?
Posted: Tue Dec 14, 2010 2:59 am
by Christopher
You can use an echo() after a die() but it will not be called. The die() function takes a parameter that is the error message.
Re: Can we use Echo after a die function?
Posted: Tue Dec 14, 2010 3:33 am
by jagannathg
Christopher wrote:You can use an echo() after a die() but it will not be called. The die() function takes a parameter that is the error message.
Hello Christopher,
Thanks a lot for your reply. Thanks for explaining me about the die() function. But the reason I have asked you about this question is, I have login page and when the users enter correct username and password which gets validated from MySQL. Then it loads a Member Page. Right now everything is working fine but I need to display those error messages like:
1. Incorrect Password
2. The user doesnt exist
3. Please enter username and password (This I have achieved using the JavaScript)
How about the first two error messages displaying on the same page.
My login page code is:
Code: Select all
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>Sanskar - Life with a difference</title>
<link rel="stylesheet" href="style.css" type="text/css" charset="utf-8" />
<script language = "Javascript">
function Validate()
{
if (document.form1.username.value == '')
{
alert('Please enter the username');
return false;
}
if (document.form1.password.value == '')
{
alert('Please enter the password');
return false;
}
}
</script>
</head>
<body>
<div id="outer">
<div id="wrapper">
<div id="nav">
<div class="clear"></div>
</div>
<div id="head">
<div id="head-left"></div>
<div id="head-right"></div>
<div id="head-1"></div>
<h1><span class="logo"><span class="top">Sanskar</span><span class="gadgets">life with a difference</span></span></h1>
</div>
<div id="head-2"></div>
<div id="login">
<div id="login-bot">
<div id="login-box">
<h2 class="login"><em>user</em>login for Sanskar</h2>
<form name="form1" method="post" action="member2.php" onsubmit="return Validate();">
<div id="login-username">
<div><label for="username">Username</label>: <input type="text" name="username"></div>
<div><label for="password">Password</label>: <input type="password" name="password"/></div>
</div>
<div id="login-button">
<input type="image" src="images/btn_login.gif" name="l" value="h"/>
</div>
<div class="clear">
<div class="reg">
New User? <a href="register.php">REGISTER for FREE</a>
</div>
</div>
</form>
</div>
<div id="login-welcome">
<div>
<h2>Welcome</h2>
<p>Don't forget to check <a href="http://www.freewebsitetemplates.com">free website templates</a> every day, because we add a new free website template almost daily.</p>
</div>
</div>
<div class="clear"></div>
</div>
</div>
</body>
</html>
My members page code is:
Code: Select all
<?php
session_start();
$host="localhost"; // Host name
$username="root"; // Mysql username
$password="rajeshwari"; // Mysql password
$db_name="test"; // Database name
$tbl_name="members"; // Table name
// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
$username = $_POST['username'];
$password = $_POST['password'];
if ($username&&$password)
{
$connect = mysql_connect("localhost","root","rajeshwari") or die ("Couldn't connect!");
mysql_select_db("test") or die ("Couldn't find db");
$query = mysql_query("SELECT * FROM userdetails WHERE username='$username'");
$query2 = mysql_query("SELECT firstname,lastname FROM userdetails WHERE username='$username'");
$result = mysql_query($query2);
$numrows = mysql_num_rows($query);
if ($numrows!=0)
{
while ($row = mysql_fetch_assoc($query))
{
$dbusername = $row['username'];
$dbpassword = $row['password'];
}
// check to see if they match!
if ($username==$dbusername&&$password==$dbpassword)
{
//$row = mysql_fetch_array($result, MYSQL_ASSOC)
//echo "{$row['firstname']}";
$_SESSION['username']=$dbusername;
echo " ";
echo "<b>You are logged in as:</b> <b>".$_SESSION['username']."</b> <a href='logout2.php'>[Logout]</a>";
$dt1 = time();
$dt2 = time();
$mysql_datetime1 = strftime("%d %B %Y", $dt1);
$mysql_datetime2 = strftime("%H:%M %p", $dt2);
echo " ";
echo "<b>Current Login Date: $mysql_datetime1</b>";
echo " ";
echo "<b>$mysql_datetime2</b>";
}
else
die ("Incorrect password!");
}
else
die("That user doesn't exist!");
}
else
die ("Please enter username and password");
?>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>Sanskar - Life with a difference</title>
<link rel="stylesheet" href="style.css" type="text/css" charset="utf-8" />
</head>
<body>
<div id="outer">
<div id="wrapper">
<div id="nav">
<div id="nav-left">
<div id="nav-right">
<ul>
<li><a href="http://www.freewebsitetemplates.com">ABOUT US</a></li>
<li><a href="http://www.freewebsitetemplates.com">PRODUCTS</a></li>
<li><a href="http://www.freewebsitetemplates.com">SERVICES</a></li>
<li><a href="http://www.freewebsitetemplates.com">SHOPPING CART</a></li>
<li><a href="http://www.freewebsitetemplates.com">NEW GADGETS</a></li>
<li><a href="http://www.freewebsitetemplates.com">REGISTER</a></li>
</ul>
</div>
</div>
<div class="clear"></div>
</div>
<div id="head">
<div id="head-left"></div>
<div id="head-right"></div>
<div id="head-1"></div>
<h1><span class="logo"><span class="top">Sanskar</span><span class="gadgets">life with a difference</span></span></h1>
</div>
<div id="head-2"></div>
</body>
</html>
Kindly help me with this.
Regards,
Jagannath
Re: Can we use Echo after a die function?
Posted: Tue Dec 14, 2010 6:07 am
by Aristona
Check error handling and exception handling.