473,386 Members | 1,830 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,386 software developers and data experts.

Authentication with $_SESSION

What is the best way to implement authentication in PHP?

Do you simply echo html if you have a valid session?

if (validate($_SESSION[var]))
{
[echo the complete page]
}

I'm new to this so I don't know of any other ways to do it. Are there many
other ways? Examples?

Thanks!
Jul 17 '05 #1
5 1832
*** deko escribió/wrote (Sun, 20 Mar 2005 01:58:55 GMT):
What is the best way to implement authentication in PHP?
"Best tool for the work". The work is not always the same ;-)
Do you simply echo html if you have a valid session?

if (validate($_SESSION[var]))
{
[echo the complete page]
}


That way users who haven't registered or logged in just see a blank page
and leave the site thinking the server is broken. You'd better redirect to
login page. Check header() and exit().
--
-+ Álvaro G. Vicario - Burgos, Spain
+- http://www.demogracia.com (la web de humor barnizada para la intemperie)
++ No envíes tu dudas a mi correo, publícalas en el grupo
-+ Do not send me your questions, post them to the group
--
Jul 17 '05 #2
"deko" <de**@hotmail.com> wrote in message
news:zN******************@newssvr14.news.prodigy.c om...
What is the best way to implement authentication in PHP?
There is no one best way. It all depends on the requirements and design of
the application. In some situations, the best way could be not to implement
authentication in PHP.
Do you simply echo html if you have a valid session?

if (validate($_SESSION[var]))
{
[echo the complete page]
}


I like to redirect to different pages depending on the outcome of the login.
My login script looks something like this:

<?

if($user = Authenticate($_POST['user'], $_POST['pass'])) {
if(Authorize($user)) {
$_SESSION['user'] = $user;
Redirect("/welcome.php");
}
else {
Redirect("/no_access.php");
}
}
else {
Redirect("/incorrect_login.php");
}

?>

At the top of all my other scripts I have something like:

RestrictAccess('read message');

The function checks the user profile stored in the session. If the user is
not authorized to perform the action in specified in the argument, then he's
redirect to the no access page. If the profile isn't even there, then he's
redirect to index.php.

Conceptually, it's useful to keep clear a distinction between authentication
and authorization. Authentication is determining whether someone is who he
claim he is. Authorization is deciding whether he can do something. The fact
that someone is authenticated should not imply that he has access.
Jul 17 '05 #3
deko wrote:
What is the best way to implement authentication in PHP?

Do you simply echo html if you have a valid session?

if (validate($_SESSION[var]))
{
[echo the complete page]
}

I'm new to this so I don't know of any other ways to do it. Are there many
other ways? Examples?

Thanks!


this is what i do, first thing:
if (validate($_SESSION[var])) {
[echo the complete page]
} else die(header("Location: $login_page_url"));

Make sure you read up on the header() function: http://www.php.net/header

--TekWiz
Jul 17 '05 #4
TekWiz wrote:
this is what i do, first thing:
if (validate($_SESSION[var])) {
[echo the complete page]
} else die(header("Location: $login_page_url"));

Make sure you read up on the header() function: http://www.php.net/header

--TekWiz


If you write it like this, you keep the code cohesive
(i means, all auth code together, application code together):

if (!validate($_SESSION[var])) die(header("Location: $login_page_url"));

[echo the complete page]

--
Nadine St-Amand
Code generator sql2php : http://www.phpbackend.com/
Jul 17 '05 #5
*** Nadine St-Amand wrote/escribió (Fri, 24 Jun 2005 01:13:31 +0000):
If you write it like this, you keep the code cohesive
(i means, all auth code together, application code together):

if (!validate($_SESSION[var])) die(header("Location: $login_page_url"));


I didn't see the original post but header() returns nothing and die()
expects a string or integer. Even if this code does work its logic is
wrong.
--
-- Álvaro G. Vicario - Burgos, Spain
-- http://bits.demogracia.com - Mi sitio sobre programación web
-- Don't e-mail me your questions, post them to the group
--
Jul 17 '05 #6

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

Similar topics

5
by: anony | last post by:
Hi, I have a site that uses forms authentication to secure the directory. The site behaves as expected when viewed externally... if I close the browser and attempt to access it again, I get...
0
by: DotNetShadow | last post by:
Hi Guys I was wondering if you can help me out with the following problem. I created a basic secure | non secure website. So I have the root directory as publically allowable pages and a...
4
by: Sosh | last post by:
Hi, I wonder if anyone knows how an application that employs remote scripting/callbacks should deal with an authentication timeout during a callback? At the moment my application just stops...
6
by: Vyoma | last post by:
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...
0
by: J | last post by:
Hello. Does anyone know if there is a way to end a user's logged on Windows Authentication session? I have some .asp files that make calls to our SQL Server which can only be accessed through...
20
by: Simon Says | last post by:
Hi, I've a login page in which after authenticating it via the Oracle DB, I will stored the user information into the Session. However, when the Session timeout occurs, all of the user...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
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
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...

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.