LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-20-2010, 09:20 AM   #1
resetreset
Senior Member
 
Registered: Mar 2008
Location: Cyberspace
Distribution: Dynebolic, Ubuntu 10.10
Posts: 1,340

Rep: Reputation: 62
How to do login-able websites?


Hi,
How do I do a website you can log in to? I've read up on cookies in PHP, but I still find the above scenario a bit hard to implement. Can anyone help me?

Thanks.
 
Old 03-20-2010, 09:33 AM   #2
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
You don't hold the details on a cookie but as part of the session variable. It can be as simple as a boolean variable held in the session variable, call it $_SESSION['loggedIn'] and set it to false. Once the user has successfully logged in change it to true. Wherever in your code you need to know if the user is logged in check this session variable.

That's the basics, obviously there is lots you can do once the basic code is in place, such as an activity check to log the user out after a certain amount of inactivity.
 
Old 03-21-2010, 03:14 PM   #3
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
it's easy to do it using apache authentication.
look at apache.org.
apache will ask for the password for you.
 
Old 03-21-2010, 03:49 PM   #4
devnull10
Member
 
Registered: Jan 2010
Location: Lancashire
Distribution: Slackware Stable
Posts: 572

Rep: Reputation: 120Reputation: 120
Cookies are generally used to maintain the login once the user has closed the browser. Sessions are generally used to maintain the login for the current... session!
So - if you are ok with just the current session (ie, user has to log in each time) then you just need to set up the session.

You will want some code to process the login, check the username/pass against a database probably and then if the credentials are ok, load the various values you want into the session variables.

I find it's often useful to create a page called "session.php" and then include() that at the top of every page I want to be secure. The code in very simplistic terms would be along the lines of:

Code:
if (!isset($_SESSION['username']))
  reDirect("./login.php"); // << I usually code a redirection function rather than writing the headers each time.
 
Old 03-22-2010, 05:20 PM   #5
CoderMan
Member
 
Registered: Jan 2009
Location: Gemini Capsule 25164
Distribution: Gentoo
Posts: 375
Blog Entries: 24

Rep: Reputation: 43
Quote:
Originally Posted by resetreset View Post
Hi,
How do I do a website you can log in to? I've read up on cookies in PHP, but I still find the above scenario a bit hard to implement. Can anyone help me?

Thanks.
PHP makes sessions easy for you. Here is an online tutorial:

http://www.tizag.com/phpT/phpsessions.php

Please keep in mind though, that if you want to program your web site in some language other than PHP, you probably want to learn the sessions system for that language. Many popular languages have session modules or libraries (like Perl's CGI::Session, for example). Best to pick your favorite language now, rather than have to adapt your code to a new language later.

[Side note: you usually don't want to simply use Apache authentication as bigearsbilly mention, because using a real sessions system allows you down the road to store other session information about the user fairly easily.]

Last edited by CoderMan; 03-22-2010 at 05:21 PM.
 
Old 03-22-2010, 05:36 PM   #6
frieza
Senior Member
 
Registered: Feb 2002
Location: harvard, il
Distribution: Ubuntu 11.4,DD-WRT micro plus ssh,lfs-6.6,Fedora 15,Fedora 16
Posts: 3,233

Rep: Reputation: 406Reputation: 406Reputation: 406Reputation: 406Reputation: 406
note you probably also would want to use a mysql database as a backend user accounts
create database for your site with a 'users' table and include a bare minimum
the user id
an md5 hash of their password

then have your login script verify against the database to see if a user is valid or not

recomended also to add some sort of privilege flag to indicate what level of access the user has to your site (either full admin access, standard access, banned etc..)

additional information can be included but not mandatory such as full name, contact info etc..

it all depends on what you want the site to do


http://us2.php.net/manual/en/book.mysqli.php (mysql improved)
http://us3.php.net/manual/en/book.mysql.php (plain mysql)

Last edited by frieza; 03-22-2010 at 05:41 PM.
 
Old 03-22-2010, 06:28 PM   #7
CoderMan
Member
 
Registered: Jan 2009
Location: Gemini Capsule 25164
Distribution: Gentoo
Posts: 375
Blog Entries: 24

Rep: Reputation: 43
PHP also makes mysql fairly easy also:

http://php.net/manual/en/book.mysql.php
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
automatic login passwords for websites gone? ieatbunnies Linux - Newbie 1 02-25-2010 10:20 PM
Can ping websites, can visit websites with ip address, cannot visit with Web address TheGeneralsLounge Linux - Networking 2 03-29-2007 02:07 AM
Best useful websites for every one gagana Linux - Software 1 10-03-2006 02:55 AM
Login to websites phil66 Ubuntu 7 02-18-2006 04:50 PM
Websites that sell websites..? mScDeX General 2 12-29-2003 03:01 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:30 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration