Page 1 of 1
A simple login form that just doesn't work!
Posted: Mon Oct 25, 2010 6:06 pm
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!
Re: A simple login form that just doesn't work!
Posted: Mon Oct 25, 2010 7:17 pm
by josh
Did you call session_start()
Re: A simple login form that just doesn't work!
Posted: Mon Oct 25, 2010 7:23 pm
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.)
Re: A simple login form that just doesn't work!
Posted: Mon Oct 25, 2010 8:00 pm
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
Re: A simple login form that just doesn't work!
Posted: Mon Oct 25, 2010 11:10 pm
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)
?>
Re: A simple login form that just doesn't work!
Posted: Mon Oct 25, 2010 11:41 pm
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.
Re: A simple login form that just doesn't work!
Posted: Tue Oct 26, 2010 12:55 am
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
Re: A simple login form that just doesn't work!
Posted: Tue Oct 26, 2010 8:20 am
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!
Re: A simple login form that just doesn't work!
Posted: Tue Oct 26, 2010 12:09 pm
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.
Re: A simple login form that just doesn't work!
Posted: Tue Oct 26, 2010 10:54 pm
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>
Re: A simple login form that just doesn't work!
Posted: Tue Oct 26, 2010 10:55 pm
by naodai
on the side, every line php code must use ; ending