Connecting Tech Pros Worldwide Help | Site Map

login function got problem

Member
 
Join Date: Jan 2007
Posts: 127
#1: Jan 18 '07
Below is my login function, but it cant display the username correctly, i don't know what is the problem. Thanx..


LOGIN FUNCTION

<?php

extract ($_POST);

mysql_connect("localhost","root","")
or die("Cannot connect to the server");

mysql_select_db("dreamzdesign")
or die("Cannot connect to the database");

$sql="select username from membership where username='$username'";

$result=mysql_query($sql)
or die("Cannot execute query1");

$num=mysql_num_rows($result);


if($num==1) //email valid
{
$sql="select username from membership where username='$username' and password='$password'";

$result2=mysql_query($sql)
or die("Cannot execute query");

$num2=mysql_num_rows($result2);
if($num2>0)
{
setcookie("Username", $username, time() + 60 * 60 * 24 * 5);

include("homepage.php");

}
else //message send to login.php when wrong password
{
$message="The username, $username exists, but you have not entered the correct password! Please try again.<br>";
include("login.php");
}
}
elseif($num==0) //message send to login.php when email not valid
{
$message="The username you entered does not exist! Please try again.<br>";
include("login.php");
}



?>

WELCOME MESSAGE

<?php
foreach ($_COOKIE as $key => $value)
?>
<h1 align="center"><? echo $value; ?>, welcome to DreamZDesign !</h1>


HOMEPAGE

<?php
foreach ($_COOKIE as $key => $value)
?>

<h1 align="center"><? echo $value; ?>, welcome to DreamZDesign !</h1>
ronverdonk's Avatar
Moderator
 
Join Date: Jul 2006
Location: The Netherlands
Posts: 4,139
#2: Jan 18 '07

re: login function got problem


Please read the Posting Guidelines before you post (code) in this forum!

moderator
Member
 
Join Date: Jan 2007
Posts: 127
#3: Jan 18 '07

re: login function got problem


Sorry. Below is my login function, welcome message and homepage. Thanx.

Login function
[PHP] <?php

extract ($_POST);

mysql_connect("localhost","root","")
or die("Cannot connect to the server");

mysql_select_db("dreamzdesign")
or die("Cannot connect to the database");

$sql="select username from membership where username='$username'";

$result=mysql_query($sql)
or die("Cannot execute query1");

$num=mysql_num_rows($result);


if($num==1) //email valid
{
$sql="select username from membership where username='$username' and password='$password'";

$result2=mysql_query($sql)
or die("Cannot execute query");

$num2=mysql_num_rows($result2);
if($num2>0)
{
setcookie("Username", $username, time() + 60 * 60 * 24 * 5);

include("homepage.php");

}
else //message send to login.php when wrong password
{
$message="The username, $username exists, but you have not entered the correct password! Please try again.<br>";
include("login.php");
}
}
elseif($num==0) //message send to login.php when email not valid
{
$message="The username you entered does not exist! Please try again.<br>";
include("login.php");
}



?>[/PHP]

Welcome message

[HTML]<html>
<head>
<title>Welcome <? echo $username; ?></title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body>

[PHP]<?php
foreach ($_COOKIE as $key => $value)
?>[/PHP]
<h1 align="center">[PHP]<? echo $value; ?>[PHP], welcome to DreamZDesign !</h1>
</body>
</html>
[/HTML]



Homepage

[PHP]<?php
foreach ($_COOKIE as $key => $value)
?>[/PHP]

[HTML]<html>
<head>
<title>DreamZDesign</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<style type="text/css">
<!--
body {
background-color: #232122;
}
body,td,th {
color: #FFFFFF;
}
a:link {
color: #FFFFFF;
}
a:visited {
color: #FFFFFF;
}
a:hover {
color: #CCCCCC;
}
a:active {
color: #CCCCCC;
}
-->
</style></head>

<body>
<p><img src="image/top%20banner.jpg" width="955" height="200"><a href="homepage.php"><img src="button/home.jpg" width="90" height="24" border="0"></a><a href="design.php"><img src="button/design%20zone.jpg" width="103" height="24" border="0"></a><a href="gallery.php"><img src="button/gallery.jpg" width="102" height="24" border="0"></a><a href="cart.php"><img src="button/cart.jpg" width="68" height="24" border="0"></a><a href="forum.php"><img src="button/forum.jpg" width="51" height="24" border="0"></a><a href="faq.php"><img src="button/faq.jpg" width="79" height="24" border="0"></a> <a href="aboutUs.php"><img src="button/about%20us.jpg" width="86" height="24" border="0"></a><a href="contactUS.php"><img src="button/contact%20us.jpg" width="121" height="24" border="0"></a>
</p><p align="center">

</p>
<h1 align="center">[PHP]<?php echo $value; ?>[/PHP], welcome to DreamZDesign !</h1>
<p><img src="image/main%20page.jpg" width="890" height="540"></p>
<p><img src="image/bottom%20banner.jpg" width="955" height="46"></p>
</body>
</html>[/HTML]
Member
 
Join Date: Jan 2007
Posts: 127
#4: Jan 18 '07

re: login function got problem


Sorry. Im new in php, below is my login page, login function, welcome message and homepage. Thanks.

LOGIN FUNCTION

[PHP]<?php

extract ($_POST);

mysql_connect("localhost","root","")
or die("Cannot connect to the server");

mysql_select_db("dreamzdesign")
or die("Cannot connect to the database");

$sql="select username from membership where username='$username'";

$result=mysql_query($sql)
or die("Cannot execute query1");

$num=mysql_num_rows($result);


if($num==1) //email valid
{
$sql="select username from membership where username='$username' and password='$password'";

$result2=mysql_query($sql)
or die("Cannot execute query");

$num2=mysql_num_rows($result2);
if($num2>0)
{
setcookie("Username", $username, time() + 60 * 60 * 24 * 5);

include("homepage.php");

}
else //message send to login.php when wrong password
{
$message="The username, $username exists, but you have not entered the correct password! Please try again.<br>";
include("login.php");
}
}
elseif($num==0) //message send to login.php when email not valid
{
$message="The username you entered does not exist! Please try again.<br>";
include("login.php");
}



?>[/PHP]


LOGIN PAGE

[HTML]<html>
<head>
<title>DreamZDesign</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<style type="text/css">
<!--
body {
background-color: #232122;
}
body,td,th {
color: #FFFFFF;
}
a:link {
color: #FFFFFF;
}
a:visited {
color: #FFFFFF;
}
a:hover {
color: #CCCCCC;
}
a:active {
color: #CCCCCC;
}
-->
</style></head>

<body>
<p><img src="image/top%20banner.jpg" width="955" height="200">
</p>
<p align="center">[PHP]<?php echo $message;?>[/PHP]You MUST login before can use our services. If you don't have username please register <a href="register.php">here.</a> </p>
<form action='do_login.php' method='post'>
<p align="center">Name : &nbsp;&nbsp;&nbsp;&nbsp;
<input type='text' name='username' size='24' maxlength='24'>
</p>
<p align="center">Password :
<input type='password' name='password' size='24' maxlength='24'>
</p>
<p></p>
<p>
<center>
<input type='submit' name='submit' value='login' />
</center>
</form>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p><img src="image/bottom%20banner.jpg" width="955" height="46"></p>
</body>
</html>[/HTML]


WELCOME MESSAGE

[HTML]<html>
<head>
<title>Welcome <? echo $username; ?></title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body>
[PHP]<?php
foreach ($_COOKIE as $key => $value)
?>[/PHP]
<h1 align="center"><? echo $value; ?>, welcome to DreamZDesign !</h1>
</body>
</html>[/HTML]

HOMEPAGE

[PHP]<?php
foreach ($_COOKIE as $key => $value)
?>
[/PHP]

[HTML]<html>
<head>
<title>DreamZDesign</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<style type="text/css">
<!--
body {
background-color: #232122;
}
body,td,th {
color: #FFFFFF;
}
a:link {
color: #FFFFFF;
}
a:visited {
color: #FFFFFF;
}
a:hover {
color: #CCCCCC;
}
a:active {
color: #CCCCCC;
}
-->
</style></head>

<body>
<p><img src="image/top%20banner.jpg" width="955" height="200"><a href="homepage.php"><img src="button/home.jpg" width="90" height="24" border="0"></a><a href="design.php"><img src="button/design%20zone.jpg" width="103" height="24" border="0"></a><a href="gallery.php"><img src="button/gallery.jpg" width="102" height="24" border="0"></a><a href="cart.php"><img src="button/cart.jpg" width="68" height="24" border="0"></a><a href="forum.php"><img src="button/forum.jpg" width="51" height="24" border="0"></a><a href="faq.php"><img src="button/faq.jpg" width="79" height="24" border="0"></a> <a href="aboutUs.php"><img src="button/about%20us.jpg" width="86" height="24" border="0"></a><a href="contactUS.php"><img src="button/contact%20us.jpg" width="121" height="24" border="0"></a>
</p><p align="center">

</p>
<h1 align="center">[PHP]<? echo $value; ?>[/PHP], welcome to DreamZDesign !</h1>
<p><img src="image/main%20page.jpg" width="890" height="540"></p>
<p><img src="image/bottom%20banner.jpg" width="955" height="46"></p>
</body>
</html>[/HTML]
Member
 
Join Date: Jan 2007
Posts: 127
#5: Jan 19 '07

re: login function got problem


What is the problem to my login function, it cant show the username correctly when user login, but it will show the name of the first user after the second user login..
what is the problem..
ronverdonk's Avatar
Moderator
 
Join Date: Jul 2006
Location: The Netherlands
Posts: 4,139
#6: Jan 19 '07

re: login function got problem


Because you include() the homepage.php script, you can address the php variable $username from it. So instead, why don't you issue a [php]echo "Welcome $username";[/php] from that homepage.php script?

Ronald :cool:
Member
 
Join Date: Jan 2007
Posts: 127
#7: Jan 20 '07

re: login function got problem


thanks.. my login function already success!! Thanks..
Reply