Problem Creating Login Script

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
Elise
Forum Newbie
Posts: 7
Joined: Sat Feb 15, 2003 12:48 am
Location: Australia

Problem Creating Login Script

Post by Elise »

I run this srcipt but I just get a not validnot valid error, when I have created the username and pass for myself on the database... what is wrong? could you please give me a hand with this.

Code: Select all

<html>
<head>
<title>validate.php</title>
</head>
<body>
<?php
	$user_name = $_POST&#1111;'user_name'];
	$password = $_POST&#1111;'password'];
	$db_user = 'mysql_user';
	$db_pass = 'mysql_pass';
	$user_name = $_POST&#1111;'user_name'];
	$password = $_POST&#1111;'password'];

	//connect to the DB and select the "dictator" database
	$connection = mysql_connect('localhost', $db_user, $db_pass) or die(mysql_error());
	mysql_select_db('triple', $connection) or die(mysql_error());

	//set up the query
	$query = "SELECT * FROM users 
			WHERE user_name='$user_name' AND password='$password'";
			
	//run the query and get the number of affected rows
	$result = mysql_query($query, $connection) or die('error making query');
	$affected_rows = mysql_num_rows($result);

	//if there's exactly one result, the user is validated. Otherwise, he's invalid
	if($affected_rows == 1) &#123;
		print 'validated';
	&#125;
	else &#123;
		print 'not valid';
	&#125;
	//...snip...
	if($affected_rows == 1) &#123;
		print 'validated';
		//add the user to our session variables
		$_SESSION&#1111;'username'] = $user_name;
	&#125;
	else &#123;
		print 'not valid';
	&#125;
?>
</body>
</html>
Thanks alot![/b]
Jaymz
Forum Newbie
Posts: 3
Joined: Sun Feb 16, 2003 7:56 am
Contact:

Post by Jaymz »

Code: Select all

$user_name = $_POST&#1111;'user_name']; 
   $password = $_POST&#1111;'password']; 
   $db_user = 'mysql_user'; 
   $db_pass = 'mysql_pass'; 
   $user_name = $_POST&#1111;'user_name']; 
   $password = $_POST&#1111;'password'];
Why do U write $user_name for 2 times? ))


First not valid - You have wrong query params and/or mysql returns 0 rows
Second one - the same as the first.

You better leave this block of code

Code: Select all

if($affected_rows == 1) 
   &#123; 
      print 'validated'; 
      $_SESSION&#1111;'username'] = $user_name; 
   &#125; 
   else 
   &#123; 
      print 'not valid'; 
   &#125;
and remove first block .

and if U have wrong username or pass U'll get only 1 error
Jaymz
Forum Newbie
Posts: 3
Joined: Sun Feb 16, 2003 7:56 am
Contact:

Post by Jaymz »

aslo

I think Your host doesn't support construction $_POST, try to use $HTTP_POST_VARS
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

I think Your host doesn't support construction $_POST, try to use $HTTP_POST_VARS
Which version of PHP are you using?

Use phpversion() to find out.

Mac
Elise
Forum Newbie
Posts: 7
Joined: Sat Feb 15, 2003 12:48 am
Location: Australia

ok

Post by Elise »

Current PHP version: 4.0.6

is the version I am using and I will just test the first sudgestions now =)
thanks!
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

Then as Jaymz suggested you cannot use $_POST as it is only available for PHP version 4.1 and up. You should have more luck with $HTTP_POST_VARS.

For more info:
http://www.php.net/manual/en/reserved.v ... ables.post

Mac
Elise
Forum Newbie
Posts: 7
Joined: Sat Feb 15, 2003 12:48 am
Location: Australia

confused

Post by Elise »

which parts should I take out? which block are you talking about
Elise
Forum Newbie
Posts: 7
Joined: Sat Feb 15, 2003 12:48 am
Location: Australia

d/w

Post by Elise »

I finally got 2 validated things =) thanks everyone, now can you sudgest a tutorial to show how to use sessions and how to now move onto a new page, the homepage.
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

You are going to have to rewrite the whole script. You need to replace $_POST with $HTTP_POST_VARS and are going to have to do your sessions differently too. You need to use the session_register() function and cannot use $_SESSION.

If you can try and get a newer version of PHP.

Mac
Elise
Forum Newbie
Posts: 7
Joined: Sat Feb 15, 2003 12:48 am
Location: Australia

hmm

Post by Elise »

I downloaded a ready configured pack because I could not work out how to compile it with mySQL seperatley.... but now I am just getting confused because it will work on my localhost but not the website
Post Reply