Need help....Sadly...

For discussions about game development that does not fit in any of the other topics.
Post Reply
Baseball435
Posts: 548
Joined: Sun May 30, 2010 3:49 am

Need help....Sadly...

Post 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
User avatar
hallsofvallhalla
Site Admin
Posts: 12026
Joined: Wed Apr 22, 2009 11:29 pm

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

Post by hallsofvallhalla »

i believe your error is here

Code: Select all

 $username=protect($player);
where is $player coming from?
User avatar
rockinliam
Posts: 466
Joined: Sun Jun 07, 2009 11:26 am

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

Post 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.
Skillset: C/C++, OpenGL, C#, Lua, PHP, MySql, Web Dev etc.
Website: https://liam-griffiths.co.uk/
Baseball435
Posts: 548
Joined: Sun May 30, 2010 3:49 am

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

Post by Baseball435 »

No I changed it to username and it still didn't work
User avatar
Callan S.
Posts: 2042
Joined: Sun Jan 24, 2010 5:43 am

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

Post 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! :)
Fight Cycle : My latest Browser game WIP
Driftwurld : My Browser Game WIP
Philosopher Gamer : My Blog
Baseball435
Posts: 548
Joined: Sun May 30, 2010 3:49 am

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

Post by Baseball435 »

Yeah I actually found that error when they told me about the first one. It still doesnt work :(
User avatar
Gate
Posts: 25
Joined: Tue Jan 25, 2011 2:35 am

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

Post 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
My software never has bugs. It just develops random features.
User avatar
PaxBritannia
Posts: 680
Joined: Sun Apr 18, 2010 1:54 pm

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

Post by PaxBritannia »

Gate wrote: These need id's
Why? Unless you're using CSS / Javascript on it, why would you need id tags?

pax.
User avatar
kaos78414
Posts: 507
Joined: Thu Jul 22, 2010 5:36 am

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

Post 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; ?>
w00t
Post Reply

Return to “General Development”