Wrox Programmer Forums
Go Back   Wrox Programmer Forums > PHP/MySQL > Beginning PHP
|
Beginning PHP Beginning-level PHP discussions. More advanced coders should post to the Pro PHP forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Beginning PHP section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old November 9th, 2004, 11:37 AM
Authorized User
 
Join Date: Oct 2004
Posts: 84
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to SiliconFuRy
Default Authentication for Idiots

The whole idea of password protecting areas of websites using PHP really does confuse me, it sounds nice and easy to program, but can anyone point me to a tutorial where by theres a small website with an area thats completely blocked, a simple log in page to unlock the rest of the pages.

I've heard many things about sessions in PHP too, but I don't think the PHP website really explains too well for a dummy like myself (im a caffiene addict, i prefer tutorials to manuals, im sure some of you can sympathise there ;)

Many shoes,

James/SiliconFuRy
__________________
Many shoes,

Jamez/SiliconFuRy
 
Old November 9th, 2004, 04:15 PM
richard.york's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 1,706
Thanks: 0
Thanked 6 Times in 6 Posts
Default

How do you want the authentication to work, do you want to restrict access to webpages or to directories (all files)?

Regards,
Rich

--
[http://www.smilingsouls.net]
Mail_IMAP: A PHP/C-Client/PEAR solution for webmail
Author: Beginning CSS: Cascading Style Sheets For Web Design
 
Old November 9th, 2004, 05:04 PM
Authorized User
 
Join Date: Jul 2004
Posts: 79
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to rajuru Send a message via Yahoo to rajuru
Default

I think he wants to restrict access to some webpages but not whole site.

Best Regard:
Md. Zakir Hossain (Raju)
www.rubd.net
www.xenex.rubd.net
www.forum.rubd.net
 
Old November 10th, 2004, 06:14 AM
Authorized User
 
Join Date: Oct 2004
Posts: 84
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to SiliconFuRy
Default

rajuru is right, just certain websites. But even a hint on how to restrict access to directories would be useful too.

Many shoes,

James
 
Old November 10th, 2004, 11:39 AM
richard.york's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 1,706
Thanks: 0
Thanked 6 Times in 6 Posts
Default

Ok, both are fairly easy to accomplish.

A login is an action that requires data to persist between connections presummably for a given amount of time. While the user remains active, the login remains.

So given the fundamentals of what a login is, you must therefore devise a way for that data to perpetuate. The best way to do this is built into PHP.

In PHP data can come from a variety of sources.

Via user input.
$_GET, $_POST and $_COOKIE

From the server, and enviornment
$_SERVER, $_ENV

 as well as be defined explicitly by the programmer
$_SESSION, $GLOBALS

These are called superglobal arrays, and you've no doubt already had some experience with these. The superglobal we're interested in is the $_SESSION superglobal. Data stored in the $_SESSION superglobal is set aside in a file on a server for a given amount of time, e.g. a session.

There are three components required to begin and use a session.

1. The function session_start() must be called at the beginning of a script before output has been made.

2. Values are set and accessed in the $_SESSION superglobal array just like any other variable.

3. A unique id is used to associate a user with their server-stored session.

Still with me? Given rule #1, any script that requires use of session data must make a call to session_start(). It must appear at the beginning of a script and there can be no white space or HTML before the opening <?php delimiter and no output from for isntance 'echo' or 'print' before the call. This is because session_start() automatically outputs a cookie containing a unique string of letters and numbers called the session id (see rule 3). The session id is used to tie one particular user to a session stored on the server.

Now given the fundamentals of what a session does and how it works you can create some test cases and experiement with what is possible.

Code:
<?php
    // Begin the session
    session_start();

    if (!isset($_SESSION['access_count']))
    {
         $_SESSION['access_count'] = 0;
    }
    else
    {
         $_SESSION['access_count']++;
    }

    echo 'You have seen this page '.$_SESSION['access_count'].' times.';
?>
The preceeding is a fairly simple demonstration. The variable $_SESSION['access_count'] increments each time that the script is accessed, then the count is output.

What if you want to link data to multiple pages?
Code:
<?php
     session_start();

     $_SESSION['foo'] = 'bar';

     echo "<a href='some_other_page.php?sid=".session_id()."'>Go to the next page</a>\n";
?>

<?php
    // some_other_page.php

    session_start();

    echo "The value of <em>foo</em> is <strong>{$_SESSION['foo']}</strong>.";
?>
The preceeding demonstrates how data persists between connections, how session_start() must be called on any page requiring the use of session data and how the session id can be passed to maintain that association.

A login script might look something like the following.
Code:
<?php
    // index.php
    session_start();

    if (!isset($_SESSION['logged_in']) || isset($_SESSION['logged_in']) && $_SESSION['logged_in'] == false)
    {
         // Do some authentication
         // Check to see if the user has submitted a login form
         // Do a database query or whatever here.
         // Check to see if whatever condition is true, i.e. the user 
         // has supplied a valid username and password.
         // Whereas this variable will be true, otherwise it is set to false,
         // but it is always set to one or the other.
         $_SESSION['logged_in'] = ($logged_in_condition)? true : false;
    }

    if ($_SESSION['logged_in'])
    { 
         // Content for registered users only.
    }
    else
    {
         // Content for users not logged in.
    }

?>
I am assuming you have an Apache HTTP server.

To protect an entire directory you can do a simple .htaccess file which will invoke an HTTP authetication scheme upon any attempts to access a directory or the children of that directory (Directives set by .htaccess are inherited).

Of course there is more than one approach to authentication.

For more information:
http://www.php.net/session
http://www.google.com/search?q=PHP+session+tutorial
http://www.google.com/search?q=htacc...authentication

HTH!

Regards,
Rich

--
[http://www.smilingsouls.net]
Mail_IMAP: A PHP/C-Client/PEAR solution for webmail
Author: Beginning CSS: Cascading Style Sheets For Web Design
 
Old November 12th, 2004, 05:38 AM
Authorized User
 
Join Date: Oct 2004
Posts: 84
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to SiliconFuRy
Default

Many thanks for all that, its very helpful.

I'm developing in a corporate environment though, so I'm having to put up with the evilness of IIS + PHP, (which, to be honest, does work perfectly fine to an extent). Thanks again for all that info

Many shoes,

James/SiliconFuRy





Similar Threads
Thread Thread Starter Forum Replies Last Post
Authentication stu9820 ASP.NET 1.0 and 1.1 Professional 4 December 7th, 2009 05:31 AM
Authentication r_ganesh76 ASP.NET 1.0 and 1.1 Professional 3 January 18th, 2008 11:34 AM
Authentication speedyH Beginning PHP 1 September 25th, 2004 11:34 AM
Authentication PbsiGuru General .NET 6 March 24th, 2004 02:41 PM
Authentication help, please CMensah Classic ASP Professional 1 November 25th, 2003 01:48 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.