Help making a login script.

Getting an unusual error with PHP

Page 1 of 1

9 Replies - 1737 Views - Last Post: 04 October 2009 - 03:16 AM Rate Topic: -----

#1 C++ Programmer  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 20
  • View blog
  • Posts: 548
  • Joined: 12-June 08

Help making a login script.

Posted 27 September 2009 - 10:53 AM

I have been making a login script for my website and I have gotten pretty far, it logs the person in and redirects them to members page, and everything, and no security yet, just going to try and see if I can get it working before adding any security, but I am getting a weird error, I will be providing all of my code for the 4 files that this script uses so there will be no problem with me not providing code, the error comes in member.php when it checks to see if they are logged in, it shows the "You must be logged in to view this page" thing but before that it tells me this

Notice: Undefined index: username in C:\wamp\www\test\member.php on line 5

Can someone please help me?

Here are the code for my files

Here is index.php

// This is index.php
<html>

	<form action='login.php' method='POST'>
			Username: <input type='text' name='username'><br>
			Password: <input type='password' name='password'><br>
			<input type='submit' value='Log In'>
	</form> <p>
	
	<a href='register.php'>Register?</a>
</html>


Here is login.php

// This is login.php
<?php

session_start();

$username = $_POST['username'];
$password = $_POST['password'];

if ($username&&$password)
{

$connect = mysql_connect("localhost","phplogin","mypasshere") or die("Couldn't connect to MySQL Database.");
mysql_select_db("phplogin") or die("Couldn't find database");

$query = mysql_query("SELECT * FROM users WHERE username='$username'");

$numrows = mysql_num_rows($query);

if ($numrows!=0)
{
	
	while ($row = mysql_fetch_assoc($query))
	{
	
	$dbusername = $row['username'];
	$dbpassword = $row['password'];
	}
	
	//check to see if they match
	if ($username==$dbusername && $password==$dbpassword)
	{
		echo "Your in! <a href='member.php'>Click here to visit the member page.</a>";
		$_SESSION['username']=$username;
	}
	else
		echo "Incorrect password!";
}
else
	die("That user doesn't exist!");
}
else
	die("You must enter a username and a password to login!");
	
?>


Here is logout.php

// This is logout.php
<?php

session_start();

session_destroy();

echo "You have been logged out. <a href='index.php'>click here to return.</a>";

?>


And finally here is member.php

// This is member.php
<?php

session_start();

if ($_SESSION['username'])
	echo "Welcome, ".$_SESSION['username']."!<br><a href='logout.php'>Logout</a>";
else
	die("You must be logged in to view this page!");
?>


Thank you for you help in advance,

- C++ Programmer

This post has been edited by C++ Programmer: 27 September 2009 - 10:54 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Help making a login script.

#2 nick1200  Icon User is offline

  • Php Coder
  • member icon

Reputation: -19
  • View blog
  • Posts: 922
  • Joined: 21-March 09

Re: Help making a login script.

Posted 27 September 2009 - 10:59 AM

http://php.about.com..._login_code.htm

that gives you the code for the register page and the login page :)
Was This Post Helpful? 0
  • +
  • -

#3 westforduk  Icon User is offline

  • D.I.C Head

Reputation: 24
  • View blog
  • Posts: 140
  • Joined: 16-August 07

Re: Help making a login script.

Posted 27 September 2009 - 11:03 AM

Looks like the variable hasn't been set when you extract the data from the database. Check that code.

Because if you set $_SESSION['username'] = "name"; on the member page it seems to work.


I will check this later after i've eaten if you still haven't fixed it :P
Was This Post Helpful? 0
  • +
  • -

#4 C++ Programmer  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 20
  • View blog
  • Posts: 548
  • Joined: 12-June 08

Re: Help making a login script.

Posted 27 September 2009 - 11:22 AM

@nick1200,

I would use that but I am trying to code this myself, thanks though :D

@westforduk,

The session variable hasn't been set? I do not get what you mean by this, explain more please?
Was This Post Helpful? 0
  • +
  • -

#5 westforduk  Icon User is offline

  • D.I.C Head

Reputation: 24
  • View blog
  • Posts: 140
  • Joined: 16-August 07

Re: Help making a login script.

Posted 27 September 2009 - 01:14 PM

I have tested this hopefully will solve your problem.

You are testing the variable like so
<?php

 if($_SESSION['username]){

	 //Do stuff

}

?>




You need to check if the variable is set using the function isset()

Change your member.php page to this and it should work. Message me if you need any further clarification. I have just put together a login system of my own. You need to add security and password protection also.




<?php
//This is member.php
session_start();


	if(isset($_SESSION['username'])){
	
			echo "Welcome, ".$_SESSION['username']."!<br><a href='logout.php'>Logout</a>";
	
	}
	else{
	
			echo "You are not logged in PLEASE LOGIN";
	
	
	}

?>



This post has been edited by westforduk: 27 September 2009 - 01:15 PM

Was This Post Helpful? 0
  • +
  • -

#6 X@MPP  Icon User is offline

  • 僕わ馬鹿ですね?
  • member icon

Reputation: 36
  • View blog
  • Posts: 1,014
  • Joined: 20-February 09

Re: Help making a login script.

Posted 27 September 2009 - 06:29 PM

i would help but i think i know what this is going to be used for so sorry
HINT: i would do SQL sanitizing to prevent SQL injection
Was This Post Helpful? 0
  • +
  • -

#7 C++ Programmer  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 20
  • View blog
  • Posts: 548
  • Joined: 12-June 08

Re: Help making a login script.

Posted 27 September 2009 - 08:25 PM

@ X@MPP

What do you think this is going to be used for?
Was This Post Helpful? 0
  • +
  • -

#8 X@MPP  Icon User is offline

  • 僕わ馬鹿ですね?
  • member icon

Reputation: 36
  • View blog
  • Posts: 1,014
  • Joined: 20-February 09

Re: Help making a login script.

Posted 02 October 2009 - 06:39 PM

View Postwestforduk, on 27 Sep, 2009 - 12:14 PM, said:

I have tested this hopefully will solve your problem.

You are testing the variable like so
<?php

 if($_SESSION['username]){

	 //Do stuff

}

?>




You need to check if the variable is set using the function isset()

Change your member.php page to this and it should work. Message me if you need any further clarification. I have just put together a login system of my own. You need to add security and password protection also.




<?php
//This is member.php
session_start();


	if(isset($_SESSION['username'])){
	
			echo "Welcome, ".$_SESSION['username']."!<br><a href='logout.php'>Logout</a>";
	
	}
	else{
	
			echo "You are not logged in PLEASE LOGIN";
	
	
	}

?>



Sorry westforduk just being nit picky
<?php

 if($_SESSION['username']){

	 //Do stuff

}

?>







<?php
//This is member.php
session_start();


	if(isset($_SESSION['username'])){
	
			echo "Welcome, ".$_SESSION['username']."!<br /><a href=\"logout.php\">Logout</a>";
	
	}
	else{
	
			echo "You are not logged in PLEASE LOGIN";
	
	
	}

?>



just fixing some quotation marks :P
and the Brake tag
Was This Post Helpful? 0
  • +
  • -

#9 chtombleson  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 22
  • Joined: 28-September 09

Re: Help making a login script.

Posted 03 October 2009 - 11:21 PM

Are you trying to give the session the users name.
If so try something like this.

In login.php when you set the session try this:
$_SESSION[$username];



This should set the session to the username.
Was This Post Helpful? 0
  • +
  • -

#10 westforduk  Icon User is offline

  • D.I.C Head

Reputation: 24
  • View blog
  • Posts: 140
  • Joined: 16-August 07

Re: Help making a login script.

Posted 04 October 2009 - 03:16 AM

@X@MPP Thanks, It was early when I did that. You get the idea :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1