13, February 2012

Help with writing a login with Sessions - webmaster forum

 
Webdigity webmaster forums
[ Home | Help | Search | Forum's Shop | Archive | Login | Register | Webmaster Directory ]
Webdigity Webmaster Forums  >  Web Development  >  PhP
Topic: Help with writing a login with Sessions
« previous next »
Pages: [1] Print
Instabuck - The easy way to sell digital products online

Author Topic: Help with writing a login with Sessions  (Read 1643 times)
Bill Cosby is my Father
*
Gender: Female
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:

Code:
<?php 
session_start
();
include(
"inc/dbconn_open.php") ;

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: Male
Posts: 6691
34714 credits
Members referred : 374


It's time to use PHP5!


« Reply #1 on: Dec 22, 2008, 08:40:30 pm »



Last blog : A new Wordpress theme for our blog
Bill Cosby is my Father
*
Gender: Female
Posts: 4
28 credits
Members referred : 0


CodeCutie


« Reply #2 on: Dec 22, 2008, 09:21:23 pm »

Thanks I will check those out.  Before you posted I kind of figured something out by changing my code to this:
Code:
<?php 
session_start
();
include(
"inc/dbconn_open.php") ;

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: Male
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


Last blog : A new Wordpress theme for our blog
Bill Cosby is my Father
*
Gender: Female
Posts: 4
28 credits
Members referred : 0


CodeCutie


« Reply #4 on: Dec 22, 2008, 10:24:08 pm »

 Embarrassed hmm I thought I was declaring _SESSION = AdminLogin
guess I need to read the tutorial on it again?
Global Moderator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 6691
34714 credits
Members referred : 374


It's time to use PHP5!


« Reply #5 on: Dec 22, 2008, 10:28:36 pm »

if you have:

$_SESSION['some_var'] = 'yes'

in file script.php

you the same variable in script2.php

if you call session_start();


Last blog : A new Wordpress theme for our blog
Global Moderator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 6691
34714 credits
Members referred : 374


It's time to use PHP5!


« Reply #6 on: Dec 22, 2008, 10:29:41 pm »

btw. your code is not safe (mysql injection risks) never use that snippet for production


Last blog : A new Wordpress theme for our blog
Bill Cosby is my Father
*
Gender: Female
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: Male
Posts: 6691
34714 credits
Members referred : 374


It's time to use PHP5!


« Reply #8 on: Dec 23, 2008, 06:37:05 am »

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...

in that case your code on page 2 is not fine


Last blog : A new Wordpress theme for our blog
I am a metal monkey!
Administrator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
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.

So this:

header('Location: Main.php');?>

will become :

header('Location: http://somesite.com/Main.php');?>

Trial and Error my two best teachers Cool
Join us @ facebook or twitter

Last blog : Butterfly Marketing 2.0
Trackback URI for this entry : http://www.webdigity.com/trackback.php?topic=8303
Tags : sesssion login Bookmark this thread : Digg Del.icio.us Dzone more....

Pages: [1] Print 
Webdigity Webmaster Forums  >  Web Development  >  PhP
Topic: Help with writing a login with Sessions
« previous next »
Jump to:
User Area
Welcome, Guest. Please login or register.
Did you miss your activation email?
Feb 13, 2012, 11:39:09 am





Login with username, password and session length

Donate to our community, and get a permanent link back to your site!

Donate to our community, and get a permanent link back to your site!






Web Design Gallery · Whois Lookup · Pagerank · Tag Browsing · Lo-fi version · Syndication · Webmaster forum history · Advertise
Developed by HumanWorks © 2005 - 2012 Webdigity webmaster community · sublime directory
Webdigity Webmaster Forums | Powered by SMF 1.0.12. © 2001-2005, Lewis Media. All Rights Reserved.