Topic: Help with writing a login with Sessions (Read 1643 times)
Bill Cosby is my Father
Gender:
Posts: 4
28 credits Members referred : 0
CodeCutie
« on: Dec 22, 2008, 05:28:18 pm »
Hey Everyone, I really could use some help with this code, I am trying to write it so it stores a session and doesn't pass the AdminID in the url after login, and I keep getting stuck because it isn't passing it to the other pages, so nothing is loading, login, get a blank page... Here is the code:
if (isset($_POST['UserName'])) {$UserName = $_POST['UserName'];} else {$UserName = '';} if (isset($_POST['Password'])) {$Password = $_POST['Password'];} else {$Password = '';}
$msg = '';
if (!empty($UserName)) {
$sql = "SELECT * FROM admin WHERE UserName='$UserName' and Password='$Password'"; $result = mysql_query ($sql); $row = mysql_fetch_object ($result);
If (mysql_num_rows($result) > 0) { $_SESSION['AdminLogin'] = "OK"; header ("Location: Main.php?AdminID=". $row->AdminID); <---how can I mask this or make it work without showing the AdminID?
} else { $msg = "Invalid Login"; } }
?>
Thanks in advance!!
Global Moderator Community Supporter?
Jedai Sword Master
Gender:
Posts: 6691
34714 credits Members referred : 374
if (isset($_POST['UserName'])) {$UserName = $_POST['UserName'];} else {$UserName = '';} if (isset($_POST['Password'])) {$Password = $_POST['Password'];} else {$Password = '';}
$msg = '';
if (!empty($UserName)) {
$sql = "SELECT * FROM admin WHERE UserName='$UserName' and Password='$Password'"; $result = mysql_query ($sql); $row = mysql_fetch_object ($result);
If (mysql_num_rows($result) > 0) { $_SESSION['AdminLogin'] = "OK"; header ("Location: Main.php?AdminID=". $_SESSION['AdminLogin']); <--now it says OK in the url....
} else { $msg = "Invalid Login"; } }
?>
Not sure if this is a real fix -- this is for a backend Intranet app, I was thinking now I may look up how to make that "OK" get masked into some random 5 digit number or something more mysterious so when the Art Dept guys get bored and attempt to break into it , this time it will be harder-- that is the reason having the AdminID in the URL became a problem...bored Art Dept...Oi... So I am guessing I have to figure out how to use MD5 ? on the "OK"
Global Moderator Community Supporter?
Jedai Sword Master
Gender:
Posts: 6691
34714 credits Members referred : 374
It's time to use PHP5!
« Reply #3 on: Dec 22, 2008, 09:26:34 pm »
if you declared a session variable, it's not necessary to pass the values via the query string
Bill Cosby is my Father
Gender:
Posts: 4
28 credits Members referred : 0
CodeCutie
« Reply #7 on: Dec 22, 2008, 11:21:58 pm »
I had tried that but then it just wouldn't pass to the next page, so when you logged in on page1.php and clicked login instead of going to page2.php it just kept reloading page1.php again...
Global Moderator Community Supporter?
Jedai Sword Master
Gender:
Posts: 6691
34714 credits Members referred : 374
I had tried that but then it just wouldn't pass to the next page, so when you logged in on page1.php and clicked login instead of going to page2.php it just kept reloading page1.php again...
I am a metal monkey!
Administrator Community Supporter?
Jedai Sword Master
Gender:
Posts: 5778
46265 credits Members referred : 3
« Reply #9 on: Dec 23, 2008, 10:55:23 am »
You should have the session_start() command in the beginning of both your php files before any output. Also as Olaf mentioned you have sql injection security problem witch can give full control over your database. Another tiny thing is that you should use full urls in location headers.