A simple login form that just doesn't work!

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
sza
Forum Newbie
Posts: 4
Joined: Mon Oct 25, 2010 5:50 pm

A simple login form that just doesn't work!

Post by sza »

Hi all,
I'm new to php so please don't punch me...

Here is a simple login page that I made and I don't understand why it doesn't work.

Code: Select all

$uname=$_POST['uname'];
$upass=$_POST['upass'];
$result=mysql_query("SELECT * FROM users where userName='".$uname."' and userPass='".$upass."'",$mysql_link);
 if (!$result)
     echo "ERROR: error occured in MySQL query.";
  else //if the query was ok...
  {
     while ($row=mysql_fetch_array($result))

session_start();
$_SESSION['IDENT'] = $row['userID'];
echo $_SESSION['IDENT'];

  }
In order to check if the script is working I've written at the end "echo $_SESSION['IDENT'];"
When I trying to log in with the correct username&password it just don't write anything.
So I wanted to figure out where is my mistake.
First, I wanted to see that the query's answer is right.
I've wrote "echo $row['userID'];" and got the right answer. (15, like in the db.)
Than, I thought that mabe I have a problem in the php engine on my computer so I just wrote the following lines:
" $_SESSION['IDENT']=15;
echo $_SESSION['IDENT']; "
And again, I got the right answer.

So, why when I write :
$_SESSION['IDENT']=$row['userID'];

the session doesn't update to 15?!?!?!?!

I just don't get it...

thanx alot!
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Re: A simple login form that just doesn't work!

Post by josh »

Did you call session_start()
sza
Forum Newbie
Posts: 4
Joined: Mon Oct 25, 2010 5:50 pm

Re: A simple login form that just doesn't work!

Post by sza »

Yes, you can see it on my original message.

The code I've added to my message is exactly what I wrote. (and session_start() is written there.)
jaceinla
Forum Commoner
Posts: 25
Joined: Thu Oct 14, 2010 12:57 pm

Re: A simple login form that just doesn't work!

Post by jaceinla »

could you also post your $mysql_link please?

When I first started using php I always thought mysql was the way to go, but since then I've been using:

Code: Select all

<?php
session_start();

$uname=$_POST['uname'];
$upass=$_POST['upass'];

$dbc = mysqli_connect('localhost','root','root','database');
$query = "SELECT * FROM users WHERE userName='{$uname}' AND userPass='{$upass} LIMIT 1'"; 

$result = mysqli_query($dbc,$query);

while($row = mysqli_fetch_array($result)) 
{
	$_SESSION['IDENT'] = $row['userID'];
}
echo $_SESSION['IDENT']
?> 

I tried this and it works...if I comment out the while loop it will still echo out the correct value, good luck
sza
Forum Newbie
Posts: 4
Joined: Mon Oct 25, 2010 5:50 pm

Re: A simple login form that just doesn't work!

Post by sza »

1. thanx alot for your post.
2. I'll post now my entire page so you'll be able to tell me if I'm doing something wrong.

page1: login.php

Code: Select all

<html>
<head>
<title>Logging in...</title>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
</head>
<body>
<?php
session_start();
include('conn.php');
$uname=$_POST['uname'];
$upass=$_POST['upass'];
$result=mysql_query("SELECT * FROM users where userName='".$uname."' and userPass='".$upass."'",$mysql_link);
 if (!$result)
     echo "ERROR: error occured in MySQL query.";
  else //if the query was ok...
  {
     while ($row=mysql_fetch_array($result))

// session_start();
	$_SESSION['IDENT'] = $row['userID'];
 //echo $_SESSION['IDENT'];

  }

mysql_close($mysql_link);
?></body></html>
page2: conn.php

Code: Select all

<?php
  $mysql_link=mysql_connect('localhost','root','password') or die("ERROR: cannot connect to MySQL server.");
  mysql_select_db('mydatabasename',$mysql_link)

?>
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Re: A simple login form that just doesn't work!

Post by josh »

Run this script a few times and describe what you see:

Code: Select all

<?php
session_start();
$_SESSION['foo']++;
var_dump($_SESSION);
After a few page loads it should start counting & prove to you that sessions work. This is called 'divide an conquer', that way you know for sure the problem is with your logic.

Also you should be using start & end braces '{' & '}' on that while loop. Omitting the braces means only the session_start() is run inside of that loop. The rest is always run.
jaceinla
Forum Commoner
Posts: 25
Joined: Thu Oct 14, 2010 12:57 pm

Re: A simple login form that just doesn't work!

Post by jaceinla »

Hi again SZA,

I'm guessing it didn't work?

Did you keep echoing out the result and comment out the setting of it? If it still echoes out the correct result then you're golden
sza
Forum Newbie
Posts: 4
Joined: Mon Oct 25, 2010 5:50 pm

Re: A simple login form that just doesn't work!

Post by sza »

Hi again Josh and Jaceinla!
Sorry for lating with my comment,
I saved the script you wrote me in a file named "try.php".
When I run it I see the output:
"array(1) { ["foo"]=> int(20) } " (and every time I refresh the number is keeping counting on...)
Is that allright?
I can't figure out what the problem is... :(

Thanx for trying to help me so far!
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Re: A simple login form that just doesn't work!

Post by josh »

That means sessions work. Now try your query:

Code: Select all

$uname=$_POST['uname'];
$upass=$_POST['upass'];
$result=mysql_query("SELECT * FROM users where userName='".$uname."' and userPass='".$upass."'",$mysql_link) or die( mysql_error() );
print_r( mysql_fetch_assoc($result) );
Call it with the same parameters. Might want to change POST to GET for testing purposes.
naodai
Forum Newbie
Posts: 3
Joined: Mon Oct 25, 2010 4:42 am

Re: A simple login form that just doesn't work!

Post by naodai »

what is error message ??
error message can help we.

why u used while get mysql select result,username,passowrd is not sole?

you can try that follow:

Code: Select all

session_start();
<html>
<head>
<title>Logging in...</title>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
</head>
<body>
<?php
include('conn.php');
$uname=$_POST['uname'];
$upass=$_POST['upass'];
$result=mysql_query("SELECT * FROM users where userName='".$uname."' and userPass='".$upass."'",$mysql_link);
 if (!$result)
     echo "ERROR: error occured in MySQL query.";
  else //if the query was ok...
  {
     $row=mysql_fetch_array($result);
// session_start();
        $_SESSION['IDENT'] = $row['userID'];
 //echo $_SESSION['IDENT'];
mysql_close($mysql_link);
  }

echo $_SESSION['IDENT'];
?></body></html>
Last edited by naodai on Tue Oct 26, 2010 11:02 pm, edited 1 time in total.
naodai
Forum Newbie
Posts: 3
Joined: Mon Oct 25, 2010 4:42 am

Re: A simple login form that just doesn't work!

Post by naodai »

on the side, every line php code must use ; ending
Post Reply