473,503 Members | 1,685 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

User Authentication and Sessions

I want to build a secure user authentication page that uses mySQL to
verify user, password, and access rights, where admins see everything
and full acess, while others see only links and pages their 'group' has
access to.

I am pretty new to PHP and this will be a great learning project, so
any guidance on where to start would be welcome. I am looking at a few
tutoriqals now, but most of them seem to focus on protecting a whole
site or single page, I want the entire thing to be displayed
dynamically depending on access rights. A good tutorial and a push in
the right direction would be just about perfect.

Hope I provided enough info, I will more thanlikely pick your brains
again :-), thanks
Jonnie

Mar 24 '06 #1
3 1526
A few examples to get you started:

At login screen:

.... ($uname and $upass from a <form>)...

$query="SELECT user_id, user_status FROM users WHERE
user_name='$uname' AND
password='$upass'";
$result=mysql_query($query);
if($result) {
if ( mysql_num_rows($result) == 0 ) {
$err[]='Username or password is incorrect.';
} else {
$row=mysql_fetch_array($result, MYSQL_ASSOC); // get status
session_start(); // send seession_id cookie to user
$_SESSION['username'] = $uname;
$status = $row[ 0 ];
$_SESSION['status'] = $status;
$url='http://' . $_SERVER['HTTP_HOST'] .
dirname($_SERVER['PHP_SELF']);
if ((substr($url, -1) == '/') OR (substr($url, -1) == '\\')){
$url = substr($url,0,-1); // Get full pathname
}
$url .= '/index.php'; // go back to index page, now logged in
header("location: $url"); // bail out to the redirect page
exit(); // and stop executing this script
......

At top of page that requires a certain level to view:
Ever page now has access to the user's status via the $_SESSION array.

session_start(); // send session_id cookie to user
if(isset($_SESSION['username'])) {
$uname=$_SESSION['username'];
$ustatus= $_SESSION['status'];
} else {
$ustatus=0;
if ( $ustatus >= $this_page_level ) { // some minimum status level to
view this page
// required to be logged in and with status high enough to
// view this screen. If not redirect to another page
$url='http://' . $_SERVER['HTTP_HOST'] .
dirname($_SERVER['PHP_SELF']);
if ((substr($url, -1) == '/') OR (substr($url, -1) == '\\')){
$url = substr($url,0,-1); // Get full pathname
}
$url .= '/login.php?'; // redirect, for example, to the login page
header("location: $url"); // bail out to the redirect page
mysql_close();
exit(); // and stop executing this script
}

Or, if you simply want a link to be invisible when the users status is
not high enough:

if ($ustatus >= $min_status)
echo '<a href="someplace.php">You can't see me.</a>';

--gary

Mar 24 '06 #2
Jonnie wrote:
I want to build a secure user authentication page that uses mySQL to
verify user, password, and access rights, where admins see everything
and full acess, while others see only links and pages their 'group' has
access to.

I am pretty new to PHP and this will be a great learning project, so
any guidance on where to start would be welcome. I am looking at a few
tutoriqals now, but most of them seem to focus on protecting a whole
site or single page, I want the entire thing to be displayed
dynamically depending on access rights. A good tutorial and a push in
the right direction would be just about perfect.

Hope I provided enough info, I will more thanlikely pick your brains
again :-), thanks
Jonnie


Or simply use mod_auth_mysql from www.sourceforge.net.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Mar 25 '06 #3
Jonnie wrote:
I want to build a secure user authentication page that uses mySQL to
verify user, password, and access rights, where admins see everything
and full acess, while others see only links and pages their 'group' has
access to.

<snip>

Possibly
1. <news:11**********************@z14g2000cwz.googleg roups.com> (
http://groups.google.com/group/comp....0fad0eef59415a )
2. http://phpgacl.sourceforge.net/

--
<?php echo 'Just another PHP saint'; ?>
Email: rrjanbiah-at-Y!com Blog: http://rajeshanbiah.blogspot.com/

Mar 25 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

15
7632
by: Joshua Beall | last post by:
Hi All, What is the best way to use a cookie to remember a logged in user? Would you store the username and password in two separate cookies? Should the password be plain text? Hashed? Not...
8
2503
by: mickeyg | last post by:
i am going through the process of password protecting a directory using ..htaccess and .htpasswd Is there a function to get the user name? Thanks
6
3329
by: Lou | last post by:
Please can someone put me out my misery! Im trying to find a multiple user/password protection script that will redirect the specific user to a specific directory. At the moment I have set up...
6
2840
by: Smitro | last post by:
Hi, I'm looking for a Tutorial about Authenticating using PHP and Linux User Accounts. Can some one point me in the right direction? Smitro
5
673
by: Rob | last post by:
I have an ASP.NET application that uses forms-based authentication. A user wishes to be able to run multiple sessions of this application simultaneously from the user's client machine. The...
2
2076
by: David Krussow | last post by:
The 2nd parameter of the method controls whether a persistent cookie is created - so that ASP.NET can "know" if the current user has been previously authenticated (during previous browser...
19
2517
by: Siobhan | last post by:
Hi What is the recommended way to store a user's database credentials across the pages of a web application so that each time the database is accessed the system doesn't have to ask them for their...
4
1790
by: =?Utf-8?B?RGVhbiBHYXJyZXR0?= | last post by:
I'm writing a .NET 2.0 app that needs to manage user sessions. We're using SQL Server 2005 tables to hold sessions status fields in a table. The problem is how to handle the situation where a user...
5
1901
by: Andy | last post by:
I have an admin section of my asp.net 2 application, and I want to be able to automatically login as a different user (Forms Authentication) whilst keeping my current session going too. The problem...
3
4323
by: =?Utf-8?B?RHVrZSAoQU4yNDcp?= | last post by:
The majority of pages on our site need authentication (forms auth against the aspnetdb database). I created an '~/auth' folder with its own config file forcing authentication for any pages in the...
0
7086
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7280
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7330
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6991
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7460
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
4672
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3167
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3154
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
736
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.