Page 1 of 1
Logging in change SESSION to 'id' from 'username'....
Posted: Wed Jul 14, 2010 9:45 am
by Vaughanjb
Hey guys.
I'm creating a login where the user logs in with there username and password. I currently have it so that it starts a SESSION with their username.
$_SESSION['username'] = $u;
I'm looking to change it to the users 'id' so that if a user has the same username there is no confusion. A problem I had in a previous pice of work.
Here is my code so far
Code: Select all
<?
session_start();
require("connection.php");
$u = $_POST['username'];
$pw = $_POST['password'];
$check = "SELECT username, password FROM users WHERE username='$u' AND password='$pw'";
$login = mysql_query($check, $connect) or die(mysql_error());
if (mysql_num_rows($login) == 1) {
$_SESSION['username'] = $u;
header("Location: home.php");
}else{ //redirects them back to login page
header("Location: login.php");
}
mysql_close($connect);
?>
Any feedback you may have will be much appreciated.
Re: Logging in change SESSION to 'id' from 'username'....
Posted: Wed Jul 14, 2010 9:53 am
by liljester
to answer your question, select the user id from the database, the save that value in the session array also. but dont you think its a very bad idea for the usernames to not be unique?!
Re: Logging in change SESSION to 'id' from 'username'....
Posted: Wed Jul 14, 2010 9:58 am
by Vaughanjb
Thanks for the quick response liljester. I did think this would be the way to do it. Just I often find it hard laying the code out.
I did wonder about the username problem, but after searching after a tutorial with no success. I thought tis would be a suitable option. If anyone could point me in the right direction for one that would be great.
Thanks again liljester.
Re: Logging in change SESSION to 'id' from 'username'....
Posted: Wed Jul 14, 2010 10:01 am
by buckit
as liljester also lead to. You should have an auto increment ID field in your user table.
to use that filed you could add the following:
Code: Select all
$row = mysql_fetch_array($login);
$_SESSION['userID'] = $row['id'];
above $_SESSION['username'] = $u;
or just replace $_SESSION['username'] = $u; all together if you no longer want that session variable.
Re: Logging in change SESSION to 'id' from 'username'....
Posted: Wed Jul 14, 2010 10:20 am
by Vaughanjb
Cheers buckit.
That confirmed the code I tried earlier. It should work but for some reason on the other pages. It doesn't I have changed all the SESSIONS to match but no joy.
My login-do.php now looks like this
Code: Select all
<?
session_start();
require("connection.php");
$u = $_POST['username'];
$pw = $_POST['password'];
$check = "SELECT username, password FROM users WHERE username='$u' AND password='$pw'";
$login = mysql_query($check, $connect) or die(mysql_error());
if (mysql_num_rows($login) == 1) {
$row = mysql_fetch_array($login);
$_SESSION['userID'] = $row['id'];
header("Location: register.php");
}else{ //redirects them back to login page
header("Location: index.php");
}
mysql_close($connect);
?>
And the top of my other pages look like this
Code: Select all
<?
session_start();
require("connection.php");
if (isset($_SESSION['userID'])){
$theUser = $_SESSION['userID'];
}
?>
Can anyone see any problems? The page runs fine but doesn't seem to be picking up the SESSION.
I have tried to echo the SESSION in the body to see what appears and each time it appears blank.
Thanks again guys
Re: Logging in change SESSION to 'id' from 'username'....
Posted: Wed Jul 14, 2010 10:27 am
by buckit
'id' is a valid column name in your user table?
try replaceing $row['id'] with something static... like 'testing'. then echo out $_SESSION['userID'] to see if it shows 'testing' if it does then you know that $row['id'] isnt returning a usable value.
Re: Logging in change SESSION to 'id' from 'username'....
Posted: Wed Jul 14, 2010 10:29 am
by Vaughanjb
Problem resolved.
The code next to the SELECT was wrong
Code: Select all
SELECT username, password FROM hub_users WHERE username='$u' AND password='$pw'";
It should have been
Code: Select all
SELECT * FROM hub_users WHERE username='$u' AND password='$pw'";
It was selecting the 'id' sowasn't creating the SESSION.
But if anyone has any link to a tutorial about individual Usernames. that would also been extremely helpful.
Thanks to buckit and liljester for your advice.
Re: Logging in change SESSION to 'id' from 'username'....
Posted: Wed Jul 14, 2010 10:36 am
by buckit
glad you worked it out!
forcing unique user names is really quite simple.
am assuming you have a signup page of some sort... and you process those post variables to add the user to the database.
before you add the user details to the database... add a check to see if the chosen username exists (just like the DB query you are using to check the username and password below). if it does then go ahead and input the data to the database... if not then return an error to the user saying he/she needs to choose a new username.