Jump to content

Need help with login page/members page


solidusjoe

Recommended Posts

Hello! I receieved some wonderful code help last time I was here so I figured this would be the best place to ask for it again! ;)

 

I am having trouble getting my login page and mebers page to work. I believe it has something to do with the way the cookies are set or something because the if(isset($_COOKIE['cookiename'])) area of the code seems to be what is not functioning correctly.

 

Here is my source:

 

user_login.php

<?php
require_once('mysql_connect.php');
//Checks if there is a login cookie

if(isset($_COOKIE['ID']))


//if there is, it logs you in and directes you to the members page
{ 
$username = $_COOKIE['ID']; 
$pass = $_COOKIE['Pass'];

$check = mysql_query("SELECT * FROM opUsers WHERE username = '$username'")or die(mysql_error());

while($info = mysql_fetch_array( $check )) 	
	{

	if ($pass != $info['password']) 
		{

		}

	else
		{
		header("Location: user_panel.php");
		}

	}

}


//if the login form is submitted

if (isset($_POST['submit'])) { // if form has been submitted


// makes sure they filled it in

	if(!$_POST['username'] | !$_POST['pass']) {
		die('You did not fill in a required field.');
	}

	// checks it against the database

	if (!get_magic_quotes_gpc()) {
		$_POST['username'] = addslashes($_POST['username']);
	}

$check = mysql_query("SELECT * FROM opUsers WHERE username = '".$_POST['username']."'")or die(mysql_error());

//Gives error if user dosen't exist

$check2 = mysql_num_rows($check);
	if ($check2 == 0) {
		die('That user does not exist in our database. <a href="register.php" target="iframe">Click Here to Register</a>');
	}


	while($info = mysql_fetch_array( $check )) 	
	{

		$_POST['pass'] = stripslashes($_POST['pass']);
		$info['password'] = stripslashes($info['password']);
		$_POST['pass'] = md5($_POST['pass']);

		//gives error if the password is wrong

		if ($_POST['pass'] != $info['password']) {
			die('Incorrect password, please try again.');
		}

		else
		{
		// if login is ok then we add a cookie 

		$_POST['username'] = stripslashes($_POST['username']);


		$hour = time() + 3600;
		$uname = $_POST['username'];
		$pname = $_POST['pass']; 
		setcookie(ID, $uname, $hour);
		setcookie(Pass, $pname, $hour);	
		//then redirect them to the members area
		header("Location: user_panel.php");
		}

	}

} 
else 
{	
// if they are not logged in
?>
<style type="text/css">
<!--
body {
background-image: url(images/bgmain.gif);
margin-left: 0px;
margin-top: 0px;
margin-right: 0px;
margin-bottom: 0px;
}
a:link {
color: #CCCCCC;
}
a:visited {
color: #999999;
}
a:hover {
color: #CCCCCC;
}
a:active {
color: #CCCCCC;
}
.style1 {font-size: 12px}
.style2 {
font-size: 12px;
font-weight: bold;
}
.style4 {font-size: 8px}
.style5 {font-size: 10px}
-->
</style>
<form name="form1" method="post" action="">
  <table width="190" height="70" border="0" align="top-left">
    <tr>
      <td width="59"><span class="style2">Username:</span></td>
      <td width="121"><input name="username" type="text" class="style2" size="20"></td>
    </tr>
    <tr>
      <td><span class="style2">Password:</span></td>
      <td><input name="pass" type="password" class="style2" size="20"></td>
    </tr>
    <tr>
      <td colspan="2"><div align="left">
        <input name="submit" type="submit" class="style4" value="Login">
      <a href="register.php" target="iframe" class="style1"><span class="style5">Not a member yet? Register here! </span></a></div></td>
    </tr>
  </table>
</form>
<?php
}


?>

 

user_panel.php(members area)

<?php

require_once('mysql_connect.php');

//checks cookies to make sure they are logged in 
if(isset($_COOKIE['ID'])) 
{ 
$username = $_COOKIE['ID']; 
$pass = $_COOKIE['Pass']; 
$check = mysql_query("SELECT * FROM opUsers WHERE username = '$username'")or die(mysql_error()); 
while($info = mysql_fetch_array( $check )) 
	{ //if the cookie has the wrong password, they are taken to the login page 
	if ($pass != $info['password']) 
		{ header("Location: user_login.php"); 
		} 
	else //otherwise they are shown the user panel
		{ 
		$check2 = mysql_query("SELECT acctbal FROM opUsers WHERE username = '$username'")or die(mysql_error()); 
		$actbal = mysql_fetch_array($check2);
?>
<style type="text/css">
<!--
body {
background-image: url(images/bgmain.gif);
margin-left: 0px;
margin-top: 0px;
margin-right: 0px;
margin-bottom: 0px;
}
a:link {
color: #CCCCCC;
}
a:visited {
color: #999999;
}
a:hover {
color: #CCCCCC;
}
a:active {
color: #CCCCCC;
}
-->
</style>
  <table width="190" height="70" border="0" align="top-left">
    <tr>
      <td><div align="center"><strong><? echo $username?></strong></div></td>
    </tr>
    <tr>
      <td><div align="center"><strong>$<? echo $actbal?></strong></div></td>
    </tr>
    <tr>
      <td><div align="center"><a href="controlpanel.php" target="iframe"> Open Control Panel</a> <a href="logout.php"><strong>Logout</strong></a> </div></td>
    </tr>
  </table>
<? 
		} 
} 
} 
else //if the cookie does not exist, they are taken to the login screen 
{ 
header("Location: user_login.php"); 
} 
?>

 

I borrowed some of this code(well a majority of it) from a tutorial on About.Com : http://php.about.com/od/finishedphp1/ss/php_login_code.htm

 

I am not very experienced with PHP yet, but each day I get a little better. This is the last major part of my site before I can officially announce it as up and running. So thank you very much in advance for any help I receive. You guys are great.

 

Sincerely,

~Joe

Link to comment
https://forums.phpfreaks.com/topic/59956-need-help-with-login-pagemembers-page/
Share on other sites

I noted what you said about if($_POST['username'] == "" || $_POST['pass'] == "")  and changed the code in my source files. Thank you.

 

Well, I'll be back in about 10 hours to check on the status of this topic, but until then I have to go to work. Thank you again everyone.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.