473,396 Members | 2,068 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Authentication session variable being lost between pages

This is quite a bit of problem I am facing, and I cannot point exactly
where I am going wrong. I have been lurking around at several forums
with regard to login and user authentication scripts and I have got as
far as this:

- Starting a session
- Registering a session variable
- Using the variable to check if the user is authenticated or not.
- Authenticating the user through MySQL database
- Logging of the user, by setting the session variable to
un-authenticated

I have been able to achive the following things too that I think is not
related to this problem:
- Encapsulate the database handling to a seperate source file
- Use a templating system of my own.
- Handle everything in only one page using the querying through URL
(this is my requirement due to the templating system I use) - I want
only one file (index.php) to be called with appropriate action requests
(?q=login or ?q=logout)

Here is the code I have so far:

----------------------------------------------------------------------------------------------------------

<?php
session_start();
session_register('auth');

require_once('database.inc');

// These $d_<somethingvariables will be placed in the template
$d_html_head = 'Some portal DART';
$d_header = 'The header - DART';
$d_status = NULL;
$d_content = NULL;
$d_nav = '<h2>Link set 1</h2><ul><li><a href="#">Link 1</a></li><li><a
href="#">Link 2</a></li><li><a href="#">Link 3</a></li></ul><h2>Link
set 2</h2><ul><li><a href="#">Link 4</a></li><li><a href="#">Link
5</a></li><li><a href="#">Link 6</a></li></ul><h2>Link set
3</h2><ul><li><a href="#">Link 7</a></li><li><a href="#">Link
8</a></li><li><a href="#">Link 9</a></li></ul>';
$d_footer = 'copyright info';
$q = '';

// Database handling part
$dartdb = new dbhandler;
$connection = $dartdb->setconnection( 'dbadmin', 'dbpassword',
'localhost');
if(!$connection)
$d_status .= "Unable to get a connection <BR /$dartdb->errorstring
<BR />";
$connection = $dartdb->setdatabase('dartdb');
if(!$connection)
$d_status .= "Unable to select DART database <BR />
$dartdb->errorstring <BR />";
if ( isset($_GET['q']) )
$q = $_GET['q'];
if ( $q == 'login')
{
// Check the 'user' and 'pass' against database and set
// 'auth' based on the result
$loginmessage = "The Employee number or the password given is wrong.
Please try again.";
$_SERVER['auth'] = 'NO';

$user = NULL;
$pass = NULL;
$user = $_POST['user'];
$pass = $_POST['pass'];
$query = "SELECT * FROM dart_emp WHERE empid = '".$user."'";
$dartdb->query($query);
if ( $user != NULL && $dartdb->result != NULL )
{
$array = $dartdb->fetch_object();
if( isset($array->empid)
&& $array->empid == $user
&& $array->password == $pass )
{
$loginmessage = "Login successful.";
$_SERVER['auth'] = 'YES';
}
}
$d_status .= $loginmessage;
}
else if ($q == 'logout')
{
// User has logged out. Hence set the 'auth' to 'NO'
$_SERVER['auth'] = 'NO';
$d_status .= 'Logged out. <BR />';
}

if( isset($_SERVER['auth']) && $_SERVER['auth'] == 'YES' )
{
$d_status .= 'Authorized access <BR />';
$d_content .= 'Content, content. <BR />Logout <A
href="?q=logout">link</A>.';
}
else
{
//Show the login form
if ($q != 'logout')
$d_status .= 'Not logged in. <BR />';
$d_content .= '<form action="?q=login" method="post" name="login">
Employee Number: <input type="text" name="user" size="6"
maxlength="6" id="user" /<BR />
Password: <input type="password" name="pass" size="30" maxlength="30"
id="pass" /<BR />
<input type="submit" name="login" value="Login" id="login" />
</form>';
}

// This is the templating system I use. The above $d_<something>
values
// are replaced in the appropriate places
require 'template/page.tpl';
?>

----------------------------------------------------------------------------------------------------------

Now, here is my problem. Once I log in, the URL will be:
http://localhost/index.php?=login

After successful login, it will show the content.
Now, if I type the http://locahost/index.php, it should still be
showing the content. But it does not. For some reason, I am loosing
the $_SERVER['auth'] variable. I am not sure, where in the flow I am
doing wrong.

Could some one please check this up and let me know what I am doing
wrong, or what more should I be including?

Please let me know, if you need anything more, or want me to explain
why I put the code as I put it there.

Regards,
Mahesh a.k.a Vyoma
http://k.mahesh.bhat.googlepages.com

Jul 30 '06 #1
6 8330

Vyoma wrote:
This is quite a bit of problem I am facing, and I cannot point exactly
where I am going wrong. I have been lurking around at several forums
with regard to login and user authentication scripts and I have got as
far as this:

- Starting a session
- Registering a session variable
- Using the variable to check if the user is authenticated or not.
- Authenticating the user through MySQL database
- Logging of the user, by setting the session variable to
un-authenticated

I have been able to achive the following things too that I think is not
related to this problem:
- Encapsulate the database handling to a seperate source file
- Use a templating system of my own.
- Handle everything in only one page using the querying through URL
(this is my requirement due to the templating system I use) - I want
only one file (index.php) to be called with appropriate action requests
(?q=login or ?q=logout)

Here is the code I have so far:

----------------------------------------------------------------------------------------------------------

<?php
session_start();
session_register('auth');

require_once('database.inc');

// These $d_<somethingvariables will be placed in the template
$d_html_head = 'Some portal DART';
$d_header = 'The header - DART';
$d_status = NULL;
$d_content = NULL;
$d_nav = '<h2>Link set 1</h2><ul><li><a href="#">Link 1</a></li><li><a
href="#">Link 2</a></li><li><a href="#">Link 3</a></li></ul><h2>Link
set 2</h2><ul><li><a href="#">Link 4</a></li><li><a href="#">Link
5</a></li><li><a href="#">Link 6</a></li></ul><h2>Link set
3</h2><ul><li><a href="#">Link 7</a></li><li><a href="#">Link
8</a></li><li><a href="#">Link 9</a></li></ul>';
$d_footer = 'copyright info';
$q = '';

// Database handling part
$dartdb = new dbhandler;
$connection = $dartdb->setconnection( 'dbadmin', 'dbpassword',
'localhost');
if(!$connection)
$d_status .= "Unable to get a connection <BR /$dartdb->errorstring
<BR />";
$connection = $dartdb->setdatabase('dartdb');
if(!$connection)
$d_status .= "Unable to select DART database <BR />
$dartdb->errorstring <BR />";
if ( isset($_GET['q']) )
$q = $_GET['q'];
if ( $q == 'login')
{
// Check the 'user' and 'pass' against database and set
// 'auth' based on the result
$loginmessage = "The Employee number or the password given is wrong.
Please try again.";
$_SERVER['auth'] = 'NO';

$user = NULL;
$pass = NULL;
$user = $_POST['user'];
$pass = $_POST['pass'];
$query = "SELECT * FROM dart_emp WHERE empid = '".$user."'";
$dartdb->query($query);
if ( $user != NULL && $dartdb->result != NULL )
{
$array = $dartdb->fetch_object();
if( isset($array->empid)
&& $array->empid == $user
&& $array->password == $pass )
{
$loginmessage = "Login successful.";
$_SERVER['auth'] = 'YES';
}
}
$d_status .= $loginmessage;
}
else if ($q == 'logout')
{
// User has logged out. Hence set the 'auth' to 'NO'
$_SERVER['auth'] = 'NO';
$d_status .= 'Logged out. <BR />';
}

if( isset($_SERVER['auth']) && $_SERVER['auth'] == 'YES' )
{
$d_status .= 'Authorized access <BR />';
$d_content .= 'Content, content. <BR />Logout <A
href="?q=logout">link</A>.';
}
else
{
//Show the login form
if ($q != 'logout')
$d_status .= 'Not logged in. <BR />';
$d_content .= '<form action="?q=login" method="post" name="login">
Employee Number: <input type="text" name="user" size="6"
maxlength="6" id="user" /<BR />
Password: <input type="password" name="pass" size="30" maxlength="30"
id="pass" /<BR />
<input type="submit" name="login" value="Login" id="login" />
</form>';
}

// This is the templating system I use. The above $d_<something>
values
// are replaced in the appropriate places
require 'template/page.tpl';
?>

----------------------------------------------------------------------------------------------------------

Now, here is my problem. Once I log in, the URL will be:
http://localhost/index.php?=login

After successful login, it will show the content.
Now, if I type the http://locahost/index.php, it should still be
showing the content. But it does not. For some reason, I am loosing
the $_SERVER['auth'] variable. I am not sure, where in the flow I am
doing wrong.

Could some one please check this up and let me know what I am doing
wrong, or what more should I be including?

Please let me know, if you need anything more, or want me to explain
why I put the code as I put it there.

Regards,
Mahesh a.k.a Vyoma
http://k.mahesh.bhat.googlepages.com
I was having this problem too, and still am, but I think it is more of
my computer than anything. Also, I hope you check your posted data
before using it in your sql.

Jul 30 '06 #2
Vyoma wrote:
After successful login, it will show the content.
Now, if I type the http://locahost/index.php, it should still be
showing the content. But it does not. For some reason, I am loosing
the $_SERVER['auth'] variable. I am not sure, where in the flow I am
doing wrong.
Should be $_SESSION['auth'], not $_SERVER['auth']. :-)

--
Toby A Inkster BSc (Hons) ARCS
Contact Me ~ http://tobyinkster.co.uk/contact

Jul 30 '06 #3
In article <11**********************@i3g2000cwc.googlegroups. com>,
Vyoma <k.***********@gmail.comwrote:
>This is quite a bit of problem I am facing, and I cannot point exactly
where I am going wrong. I have been lurking around at several forums
with regard to login and user authentication scripts and I have got as
far as this:

- Starting a session
- Registering a session variable
Use of session_register() is deprecated, according to the documentation
at http://us2.php.net/manual/en/functio...n-register.php

It's also a good idea to call statements similar to these before you
call session_start():

session_save_path(/usr/home/my_account/public_html/sessions");
session_name('login_ID');

....where the path is a path on your server to your directory space, and
the login ID is something that describes the cookie being set on the
visitor's browser.
><?php
session_start();
session_register('auth');
session_register() isn't needed, or if you want you could say
$_SESSION['auth'] = NULL;
here instead.

Later on you have this:
$_SERVER['auth'] = 'YES';
That should be
$_SESSION['auth'] = 'YES';

and here:
>if( isset($_SERVER['auth']) && $_SERVER['auth'] == 'YES' )
Replace SERVER with SESSION.

-Alex
Jul 30 '06 #4
Ah! I feel so dumb. I should have used that _SESSION instead of
_SERVER. It is working like a clockwork now, and I do not have any
problems.

As stated above, I checked the PHP.net site for the session_register()
fucntion. It is indeed deprecated. Now, I have a couple of questions
more:

So, how should I proceed?
If I do not call the session_register('auth'), can I still use
_SESSION['auth']?

And if I am not using session_register(), should I be calling
session_start()?

Regards,
Vyoma

Aug 1 '06 #5

Vyoma wrote:
Ah! I feel so dumb. I should have used that _SESSION instead of
_SERVER. It is working like a clockwork now, and I do not have any
problems.

As stated above, I checked the PHP.net site for the session_register()
fucntion. It is indeed deprecated. Now, I have a couple of questions
more:

So, how should I proceed?
If I do not call the session_register('auth'), can I still use
_SESSION['auth']?

And if I am not using session_register(), should I be calling
session_start()?

Regards,
Vyoma
Oops again. I did not read the last mail before replying.

Thanks Alex. You have answered to all the questions I posted last.

-Vyoma

Aug 1 '06 #6
Vyoma wrote:
If I do not call the session_register('auth'), can I still use
_SESSION['auth']?
Yes -- session_register() effectively does nothing these days.
And if I am not using session_register(), should I be calling
session_start()?
Yes -- session_start() is still needed.

--
Toby A Inkster BSc (Hons) ARCS
Contact Me ~ http://tobyinkster.co.uk/contact

Aug 2 '06 #7

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

Similar topics

3
by: Peter Row | last post by:
Hi, I better get the background stuff out the way first, so here goes: - Porting a VB6 webclass app to VB.NET using HttpHandlers and FormsAuthentication - When someone visits my site...
1
by: Werner | last post by:
Hi Patrick! Can you give an example of how to use a frameset inside an aspx-file? When I create a new frameset in Visual Studio.Net it just gives me a htm-File. Or give me a link where I can...
2
by: Jim | last post by:
Hi, I am using forms based authentication for my website. Whilst testing I have noticed that occasionaly it appears that the Context.User.Identity.Name is valid however the session variables...
2
by: Shapper | last post by:
Hello, I am creating a web site with a Content Management System. The web site doesn't have any restricted area. The CMS has a login page and all pages in it need the user to be...
18
by: Rippo | last post by:
Hi I am using role base forms authentication in asp.net and have come across a problem that I would like advice on. On a successful login a session variable is set to identify a user. This is...
3
by: kpg | last post by:
ASP.NET 2.0 I have an unusual situation dealing with forms authentication, not doubt brought on by how I have structured the application. The setup: Users enter the site from one of several...
13
by: SAL | last post by:
Okay, don't bash me to hard for my design on this app, it's my first web app and it's in production. My basic design is using Datatables created via the designer with a business logic class in...
4
by: Bjorn Sagbakken | last post by:
In a web-application with login creds (user, pwd), these are checked against a user table on a SQL server. On a positive validation I have saved the userID, name, custno and role-settings in a...
5
by: Rory Becker | last post by:
Having now created a Custom MembershipProvider that seems to work correctly with my Logon and ChangePassword controls, I am, as they say, a happy bunny. The next stange is to move on to the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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
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...
0
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
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...

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.