Page 1 of 1

Need help....Sadly...

Posted: Mon Nov 15, 2010 9:12 pm
by Baseball435
Hey everyone. Well im trying to get this fricken login and register thing working (i know it's super nooby) but i just cant get it to work and idk why. Im just going to give you the code i have so far.

index.php:

Code: Select all

<?php 
session_start();

if (isset($_SESSION['username'])) {
		echo $_SESSION['errors'];
		unset($_SESSION['errors']);
	} else {  
?>
<head>
	<title>Login</title>
	<body>
	<CENTER>
	Login: <br />
	<form method="post" action="login.php"><br />
	Username: <input type="text" name="username"><br />
	Password: <input type="password" name="password"><br />
	<input type="submit" value="Log In"><br />
	</form>
	<a href="register.php">Register</a>
	</CENTER>
	</body>
</head>
	<?php } ?>
Login.php:

Code: Select all

<?php
session_start();

$connect=mysql_connect("localhost","root", "");
mysql_select_db("phpwebsite",$connect) or 
die (mysql_errno().":<b> ".mysql_error()."</b>");


	function protect($value){
		$value = mysql_real_escape_string($value);
		$value = stripslashes($value);
		$value = strip_tags($value);
	}


		$username=$_POST['username'];

		$password=$_POST['password'];

		$username=protect($player);

		$password=protect($password);

		$query = "SELECT username,password FROM users WHERE username='$player' and password='$password'"; 

		$check = mysql_query($query) or die("Could not conect to database") ;

		$check2=mysql_fetch_array($check);

		

		if($check2)

		{

       		$_SESSION['username']=$username;
       		$_SESSION['errors']="<center>Thank You For Logging In! <br />Welcome!</center>";

		}

		else

		{

			  	$_SESSION['errors']="Wrong username or password. <A href='index.php'>Try Again</a>";

		}
		
	header('Location:index.php');

?>
Im also trying to use sessions and thats, i think, my main problem. I will type in my username and password (which is correct) and press submit it just goes back to my index page and still displays the login form. I really dont know why the session is not working. Thanks guys.

~baseball435

P.S: Oh yeah and i already have the register working, that was easy lol :P

Re: Need help....Sadly...

Posted: Mon Nov 15, 2010 10:08 pm
by hallsofvallhalla
i believe your error is here

Code: Select all

 $username=protect($player);
where is $player coming from?

Re: Need help....Sadly...

Posted: Mon Nov 15, 2010 10:43 pm
by rockinliam
I second halls on that one.
Should it not be username there?

Edit: I changed this post, what i put orginally was wrong, my bad lol.

Re: Need help....Sadly...

Posted: Mon Nov 15, 2010 11:09 pm
by Baseball435
No I changed it to username and it still didn't work

Re: Need help....Sadly...

Posted: Tue Nov 16, 2010 8:29 am
by Callan S.

Code: Select all

$query = "SELECT username,password FROM users WHERE username='$player' and password='$password'";
Again used you used $player instead of $username. Because of it that $check2 doesn't fire (as it doesn't find anything), so it doesn't set the session and you just go back to log in. Debugging cookie, please! :)

Re: Need help....Sadly...

Posted: Tue Nov 16, 2010 11:34 am
by Baseball435
Yeah I actually found that error when they told me about the first one. It still doesnt work :(

Re: Need help....Sadly...

Posted: Sat Jan 29, 2011 4:41 am
by Gate

Code: Select all

Username: <input type="text" name="username"><br />
Password: <input type="password" name="password"><br />
These need id's

Code: Select all

Username: <input type="text" name="username" id="username"><br />
Password: <input type="password" name="password" id="password"><br />


To check if your posted data is going through correctly you should consider getting the firefox plugin https://addons.mozilla.org/en-us/firefo ... mper-data/

Really good for checking to post data is going through correctly and in the format you need

Re: Need help....Sadly...

Posted: Sat Jan 29, 2011 12:01 pm
by PaxBritannia
Gate wrote: These need id's
Why? Unless you're using CSS / Javascript on it, why would you need id tags?

pax.

Re: Need help....Sadly...

Posted: Sat Jan 29, 2011 10:12 pm
by kaos78414
Add

Code: Select all

echo $_SESSION['errors'];
to the else part of index.php to see if you're putting in the right username and password.

EDIT: I'd also like to mention when you're splitting up if/else with html like that, you can do this:

Code: Select all

<?php if ($some_var) : ?>
<!-- Some HTML -->
<?php else : ?>
<!-- Some different HTML -->
<?php endif; ?>
Similarly, you can do foreach statements the same way:

Code: Select all

<?php foreach($thing as $row) : ?>
<!-- Do something with it -->
<?php endforeach; ?>