Hello everyone,
I am trying to make my login script and have tried two different alternatives.
<html>
<head>
<title>HomePage</title>
<style type="text/css">
h1
{
font-family=Times;
font-size:50;
font-weight:bolder;
text-align:center;
padding:5px 0 5px 30px;
margin-bottom:19px;
font-size:2em;
color:#fff;
}
ul
{
list-style-type:none;
margin:0;
padding:0;
overflow:hidden;
}
li
{
float:left;
}
a:link,a:visited
{
display:inline;
width:250px;
font-weight:bold;
color:#FFFFFF;
background-color:003366;
text-align:center;
padding:6px;
font-style:italic;
text-decoration:none;
}
a:hover,a:active
{
background-color:#7A991A;
}
h2
{
text-align:center; color:#ff9841; font-weight:bold;
}
div
{
color:#FFFFFF;
font-family=Times>;
font-size:15;
}
#header
{
height:175px; padding:5px 0; margin-bottom:15px;background-color:black;
}
#footer
{
height:80px; padding:5px 0; margin-bottom:15px;background-color:black;
}
</style>
<body bgcolor="gray" text="#FFFFFF" link="#0000FF" vlink="#800080" alink="#FF0000" background="Blue.jpg">
<div id="header">
<h1>Stock-Dissect</h1>
<ul>
<li><a href="Aboutus.html">About Us</a></li>
<li><a href="#news">What's this all about?</a></li>
<li><a href="#contact">Features</a></li>
<li><a href="#jkf">Register</a></li>
</ul>
</div>
<?php
mysql_connect("localhost", "root", "root") or die(mysql_error());
mysql_select_db("myDb") or die(mysql_error());
//Checks if there is a login cookie
if(isset($_COOKIE['ID_my_site']))
//if there is, it logs you in and directes you to the members page
{
$username = $_COOKIE['ID_my_site'];
$pass = $_COOKIE['Key_my_site'];
$check = mysql_query("SELECT * FROM users WHERE username = '$username'")or die(mysql_error());
while($info = mysql_fetch_array( $check ))
{
if ($pass != $info['password'])
{
header("Location: HomePage.php");
}
}
}
//if the login form is submitted
if (isset($_POST['submit']))
{
// makes sure they filled it in
if(!$_POST['Enterhere'] || !$_POST['password'])
{
die('You did not fill in a required field.');
}
// checks it against the database
$check = mysql_query("SELECT * FROM users WHERE username = '".$_POST['Enterhere']."'")or die(mysql_error());
//Gives error if user dosen't exist
$check2 = mysql_num_rows($check);
if ($check2 == 0)
{
die('Sorry wrong username entered. Click here to register -> <a href=Register.php>Register Now</a>');
}
while($info = mysql_fetch_array( $check ))
{
//gives error if the password is wrong
if ($_POST['password'] != $info['Password'])
{
die('Incorrect password, please try again.');
}
else
{
// if login is ok then we add a cookie
$hour = time() + 3600;
setcookie(ID_my_site, $_POST['Enterhere'], $hour);
setcookie(Key_my_site, $_POST['password'], $hour);
//then redirect them to the members area
header("Location: CompanyPage.php");
}
}
}
else if(!isset($_POST['submit']))
{
// if they are not logged in
echo "Please try again";
?>
<fieldset>
<legend style="color:#FFFFFF" style=font-weight:"bold">
Log in:
</legend>
<form action="checklogin.php" method="post">
Username: <input type="text" name="Enterhere " /><br/><br/>
Password: <input type="password" name="Password" /><br/><br/>
<input type="Submit" value="Submit" />
</form>
</fieldset></br></br></br>
<?php
}
?>
<div id="footer">
<p>Footer *****</p>
</div>
</body>
</html>
SECOND ONE.
<html>
<head>
<title>HomePage</title>
<style type="text/css">
h1
{
font-family=Times;
font-size:50;
font-weight:bolder;
text-align:center;
padding:5px 0 5px 30px;
margin-bottom:19px;
font-size:2em;
color:#fff;
}
ul
{
list-style-type:none;
margin:0;
padding:0;
overflow:hidden;
}
li
{
float:left;
}
a:link,a:visited
{
display:inline;
width:250px;
font-weight:bold;
color:#FFFFFF;
background-color:003366;
text-align:center;
padding:6px;
font-style:italic;
text-decoration:none;
}
a:hover,a:active
{
background-color:#7A991A;
}
h2
{
text-align:center; color:#ff9841; font-weight:bold;
}
div
{
color:#FFFFFF;
font-family=Times>;
font-size:15;
}
#header
{
height:175px; padding:5px 0; margin-bottom:15px;background-color:black;
}
#footer
{
height:80px; padding:5px 0; margin-bottom:15px;background-color:black;
}
</style>
<body bgcolor="gray" text="#FFFFFF" link="#0000FF" vlink="#800080" alink="#FF0000" background="Blue.jpg">
<div id="header">
<h1>Stock-Dissect</h1>
<ul>
<li><a href="Aboutus.html">About Us</a></li>
<li><a href="#news">What's this all about?</a></li>
<li><a href="#contact">Features</a></li>
<li><a href="#jkf">Register</a></li>
</ul>
</div>
<?php
mysql_connect("localhost", "root", "root") or die(mysql_error());
mysql_select_db("myDb") or die(mysql_error());
$myusername=$_POST['Enterhere'];
$mypassword=$_POST['mypassword'];
$sql="SELECT * FROM $tbl_name WHERE UserName='$myusername' and Password='$mypassword'";
$result=mysql_query($sql);
// Mysql_num_row is counting table row
$count=mysql_num_rows($result);
// If result matched $myusername and $mypassword, table row must be 1 row
if($count==1){
// Register $myusername, $mypassword and redirect to file "loginsuccess.php"
session_register("myusername");
session_register("mypassword");
header("location:login_success.php");
}
else {
echo "Wrong Username or Password";
}
?>
<div id="footer">
<p>Footer *****</p>
</div>
</body>
</html>
-------------------------------------------------------------------------------------------------------
BOTH OF THESE GO STRAIGHT TO THE ELSE STATEMENT, AND DISPLAY WRONG USERNAME AND PASSWORD OR DISPLAY THE FORM. PLEASE HELP