Jul 26, 2008

User Login System With Setcookies

Free Web Hosting, No Ads > CONTRIBUTE > Computers > Programming Languages > PHP Programming
Pages: 1, 2

free web hosting

User Login System With Setcookies

Raptrex
a friend of mine is quite good at php and told me not to use sessions and to use setcookie

im not sure how to use setcookie to make a user authentication system and was wondering if anyone here know a tutorial on how to do it

Reply

HmmZ
I wrote a tutorial about it (to give the tutorial some more perspective I wrote the full authentication system), It's pending right now, so you'll have to wait until some mod validates it tongue.gif

I'll post the link once its validated.

Reply

Raptrex
ok i will wait for your reply
i hope its easy to use smile.gif

Reply

SystemWisdom
QUOTE(Raptrex @ Jun 10 2005, 11:58 PM)
a friend of mine is quite good at php and told me not to use sessions and to use setcookie
*



Sessions are more secure than cookies.. Cookies get passed back and forth from the client to the server, and can easily be caught as it goes along the network. Anything that is considered 'sensitive' material/data would need to be encrypted before being stored in a cookie.
If shared hosts (like Trap17) concern you and/or you want to avoid possible Session Hijacking techniques, then a better way to protect your sessions is to setup a Custom Session Handler using a DB and store all session data in the DB. Then, with every user privelage escalation (like a Login) you simply regenerate the users Session ID to prevent it from being Hijacked.

Also, storing Sessions in a cookie presents other problems as well. Some people disable cookies, forcing get/post alternatives (ever see a long encrypted SessionID in your URL bar?), which could limit the user from seeing your site altogether. Also, many browsers limit the size of the cookies they accept, and to be on the safe side, you should not exceed 4kb (4096 bytes) in a single cookie, otherwise some browsers may truncate the cookie data.

Overall, I would recommend utilizing both methods to maximize your security, and you can read up on both of these methods and more at PHP Security Consortium.

@Hmmz:
Is it the one entitled "Incredible Secure Authentication"? I would like to see what types of security measures you have considered in your tutorial. I am writing a tutorial myself on Secure Authentication and is quite large to say the least. I am curious to see if our systems are very similar or very distinct! tongue.gif
If it is too similar, I guess you beat me to posting then, 'cause I wouldn't want to post anything like a copy-cat tutorial! tongue.gif
I look forward to reading it! smile.gif

 

 

 


Reply

Raptrex
well with cookies, the site remembers you, but with session, it only remembers you until you close your browser or something like that

anyway i found a pretty decent tutorial and was wondering how i can make a "logout" script and a "whos online" script

http://www.xentrik.net/php/signup/complete.php

the scripts works

see it here

nothing fancy
just added if your logged in it would show you
oh and also say your not logged in, i want it to show the login form but i dont know how to use the if then statement that much

thx smile.gif

Reply

guangdian
there will be more lots of things you would need to learn..that' s true.i can't know so many code of php i just install lots of php programme then they all use cookies,but not dangerous

Reply

Raptrex
ok i found a little script
CODE
<?php
// grab current time
$time=time();

// handle the logout event
if ($logout == true) {
setcookie ("user", md5($_POST[user]), $time-3200);
setcookie ("pass", md5($_POST[pass]), $time-3200);
echo "<a href=http://www.pro.trap17.com/>Logged Out!</a>";
}

// handle validation event
if ($_POST[user] && $_POST[pass]) {
mysql_connect(localhost, raptrex_forum, forum) or die(mysql_error()); // Connection
mysql_select_db(raptrex_member) or die(mysql_error()); // Selection of database
$user_data = mysql_fetch_array(mysql_query("select id, username, password from users where username='$_POST[user]' and password='$_POST[pass]'"));
if ($user_data[id] > 0) {
 setcookie ("user", md5($user_data[username]), $time+3200);
 setcookie ("pass", md5($user_data[password]), $time+3200);
 echo "<a href=http://www.pro.trap17.com>Logged In!</a>";
} else { $login_error= true; }
}

// handle login event, both successful and erroneous, or show login screen
if ($login_error == true) { ?>
<table align=center style="font-family:arial; font-size:12; border:1 solid #000000;">
 <tr><td align=center bgcolor=#123dd4>LOGIN ERROR</td></tr>
 <tr><td align=center><b>Invalid Username and/or Password</b><br><br><a href=login.php>Back</a></td></tr>
</table>
<?
} elseif ($_COOKIE[user] == md5($username) && $_COOKIE[pass] == md5($password)) { ?>
<table align=center style="font-family:arial; font-size:12; border:1 solid #000000;">
 <tr><td align=center bgcolor=#123dd4>SECURE AREA</td></tr>
 <tr><td align=right><a href=login.php?logout=true>Logout</a></td></tr>
 <tr><td>You have successfully logged in.<br><br>
  Encrypted Username: <b><?=  $_COOKIE[user] ?></b><br>
  Encrypted Password: <b><?= $_COOKIE[pass] ?></b><br>
 </td></tr>
</table>
<?
} else {
?>
<form action=login.php method=post>
<table align=center style="font-family:arial; font-size:12; border:1 solid #000000;">
 <tr><td colspan=2 align=center bgcolor=#123dd4>LOGIN</td></tr>
 <tr><td align=right>Username: </td><td><input type=text name=user size=15></td></tr>
 <tr><td align=right>Password: </td><td><input type=password name=pass size=15></td></tr>
 <tr><td align=center colspan=2><input type=submit value=Login></td></tr>
</table>
</form>
<?
}
?>


im going to include this onto my site
say if im not logged in, it shows the login area
but if im logged in, it says im logged in as whoever im logged in as
how do i do this?


Reply

FaLgoR
Man, I made a script an post here a looooooooooong time ago. I think the Title was: Login Sistem and Subtitle: With PHP + MySQL. It was a really long time ago. Try to use the search engine.The script is complete, with login, signup, administration, profile, bla blah blah blah smile.gif It uses cookies and MySQL to save the informations. If I find the link, I'll post here.

Reply

Raptrex
http://www.joe2torials.com/php/php_cookies_remember_me.php

i found that script that uses sessions and cookies

and was wondering if i was logged in it would say "Logged in as Raptrex"
and if i wasnt it would show the login form

ive tried and it hasnt worked

Reply

Raptrex
QUOTE
I wrote a tutorial about it (to give the tutorial some more perspective I wrote the full authentication system), It's pending right now, so you'll have to wait until some mod validates it tongue.gif

I'll post the link once its validated.


hmmz did your tutorial ever get validated cuz i havent seen it in the tutorial section lately

Reply

Latest Entries

hype
This look original to me, I've never seen this code, I believe its original and that's the bad point of having too many moderator(no hard fellings!)...

Maybe try explaining to the admins and show them your work, and ask for justice!! wink.gif

Reply

HmmZ
Well, i didn't save it or anything so ill have to start completely over..here goes the 'short' version ohmy.gif

Step 1: Connect, login and authenticate
Of course, before you start authenticating a user you need a login form that ultimately suits the authentication process, and a config file that sets up a connection to your mysql database and the therein situated usertable, those are a basic thing but do the trick and are self-explanatory:

config.php
CODE
<?

$server = "host";
$database = "database name";
$db_user = "db username";
$db_pass = "db password";
$table = "usertable";
?>


logform.php
CODE
<form action="login.php" method="post">
Username: <input type="text" name="username" size="15">
Password: <input type="password" name="password" size="15">
<input type="submit" value="Log In">
</form>


Then you need to create the login.php, wich basically is your authentication page, ill explain everything after the code...:

login.php
CODE
<?

ob_start();
include("config.php");

// connect to the mysql server
$link = mysql_connect($server, $db_user, $db_pass)
or die ("Could not connect to server..");

// select the database
mysql_select_db($database)
or die ("Could not select database");
$match = "select id from $table where username = '".$_POST['username']."'
and password = '".$_POST['password']."';";
$qry = mysql_query($match)
or die ("Could not match data because ".mysql_error());
$num_rows = mysql_num_rows($qry);

if ($num_rows <= 0) {
echo "Sorry, there is no username $username with the specified password.<br>";
echo "<a href=log_form.php>Try again</a>";
exit;
} else {
setcookie("loggedin", "TRUE", time()+(900 * 1));
setcookie("username", "$username");
echo "You are now logged in!<br>";
echo "Continue to the <a href=members.php>Members</a> area.";
}
ob_end_flush();

?>

allright,
ob_flush() is a function used in php to send the output of the content, known as the output buffer, in this script, it basically sends the output of the authentication to the database, following a full check of the send data.

then the script includes config.php, wich is the file used to connect to the server,database and ultimately the table.

$link is the variable that actually connects to the database using variables assigned in config.php.

then you have to select the database where the usertable is situated following the query to 'get' the username and password inserted in the login fields, then it checks if there's a match, if so, it gives the user the link to the members area, if not, it displays a login error.

with a successful login it also sets 2 cookies, one for the successful login and 1 for the user itself, within the usercookie, it also sets the variable $username, if you now anywhere wanna display the users username, you don't have to assign a whole new variable, all you need is $username, wich basically displays the username used with the login.


Step 1: members area code
We've gone through the whole login and authentication process, but we of course need something on each members page that recognizes and validates the user, this small code checks if the cookie is valid and disconnects or connects (continued)the user if valid or invalid:
CODE
<?
if (!isset($_COOKIE['loggedin'])) die("You are disconnected!  <a href=\"log_form.php\">Click here</a>"); $username = $HTTP_COOKIE_VARS["username"]; echo "You are connected! « $username »";
?>


Put that small piece of code at the top of every members page to secure your members pages.

Thats about it, Hope this helps..
and NO dooga, this is not ripped or anything so don't accuse me again, ive never ripped before and i like to keep it that way..

Reply

Raptrex
could you post it here?
you'll still get hosting credits smile.gif

Reply



Got an Opinion! Express your Views! (no registration):-
Add your Reply/ Opinion/ Views/ Comments/ Suggestion/ Questions/ Queries etc.
Posts with decent grammar & English will be accepted and please refrain from profanities.
For asking a Question, We recommend you to sign-up (for free) so that you can track the topic easily.

Nature of your Post*: Opinion/ Reply/ Comments
Question/Query
Feedback to us.
       
Name   Email
Title/Question*

(Maximum characters: 10,000)
You have characters left.
Confirm Code:

Pages: 1, 2
Recent Queries:-
  1. setcookies php - 13.58 hr back. (1)
  2. encrypted php login system using cookies - 62.88 hr back. (1)
  3. how to logen password and user name to database in php - 65.56 hr back. (1)
  4. coding how to connect to database using php in easyphp with register login form - 116.79 hr back. (1)
  5. phpbb3 "one login" script - 125.07 hr back. (1)
  6. making a signup login system with php - 135.04 hr back. (1)
  7. setcookies - 165.17 hr back. (1)
  8. i need a username and a password please for fsm directory - 177.79 hr back. (1)
  9. php upload "with login" - 184.09 hr back. (1)
  10. cache:q7o30nbf0hej:www.trap17.com/index.php/sessions-login_t38315.html "php include" session cookie "curl" - 184.86 hr back. (1)
Similar Topics

Keywords : user, login, system, setcookies

  1. Unexpected T_string In User.php [resolved]
    (5)
  2. Php Ftp Upload Form
    Adding User Directory to PHP Upload Form - Help (0)
    Alright I am trying to have a PHP FTP Upload Form that allows the user to create the directory
    folder for where they want to upload there files to. example: Main Directory: vainsoft.com There
    directory: vainsoft.com/modeling or vainsoft.com/photography But I dont want them to be able to
    upload things into the main directory, only sub-directories, is that possible with this coding that
    I have: //uses $_FILES global array //see manual for older PHP version info //This
    function will be used to get the extension from the filename function get_extension($fi....
  3. One Login Account At One Time
    (3)
    When we login, we got a session. But at the same time when we still login, another one could login
    at our account. So, can we restrict only one user could login at one account? So if another wants
    to log into the same account, he must wait until the one logs out. Thanks in advance.....
  4. [mysql]get Id Of Loged In User?
    (7)
    how to get the id number of the loged in user? my db is id. username. password. i have tryed a
    few things.. but i never seem to get it right /ohmy.gif" style="vertical-align:middle" emoid=":o"
    border="0" alt="ohmy.gif" />....
  5. Html Site With Login
    Is it possible? (2)
    Hello. I´m building my own site and I need some help... Is it possible to use a login sistem in php
    and mysql database in a html site? ....
  6. Login System
    (6)
    i am designing a site for my alliance for the game Dark Throne. and i want some content to be
    availabe to members of the alliance only, and other content to be available to people with a certain
    rank within the alliance. i know this should be somewhat simple, but i am not that sure how to do
    it. my idea for the website is just have basic info about the alliance available to everyone, then
    news about the alliance and member lists and other things like that available to every alliance
    member, then things such as the strike team, diplomat team, and special areas like that,....
  7. Compare 2 List Of User Ids From Different Tables
    (1)
    Hi all. I am trying to make a list out of 2 list. The first list is a complete list of users id
    from users table (List A) The Second list is of users id from another table (List /cool.gif"
    style="vertical-align:middle" emoid="B)" border="0" alt="cool.gif" /> I want to subtrack the user
    ids in List B from List A and make List C. Thanks in advance for any help /smile.gif"
    style="vertical-align:middle" emoid=":)" border="0" alt="smile.gif" /> Update: I looked up a old
    sql book i had and found some query examples. I tried the EXCEPT statement in mysql but it dont wor....
  8. Php Login Script
    (1)
    I'm looking for a good php login script. I would like one where it pops up. like http-auth. but
    with out the data base. I would also like for it to have a log out fuction.....
  9. Creating A Login Box That Links To My Phpbb Forum
    Have my phpBB Forum Intergrated with my Website (4)
    Can someone please give me a code that I can use to put a login box on my website, that will login a
    user into my phpBB Forum? Sort of like Having my phpBB Forum Intergrated with my Website? Thank you
    so much if you can! /angel.gif" style="vertical-align:middle" emoid=":angel:" border="0"
    alt="angel.gif" /> Ex. ....
  10. Multiple Drop Down Lists ?
    Multiple drop down lists to take user to new page (4)
    Hi everyone I was wondering if anyone could help. I want to create a page with multiple drop down
    lists and depending what the user selects decides the page they will be taken to. Sorry i havent
    explained this too well. Here is an example of what i want (link below) the user is also emailed a
    copy http://www.dermalogica.com/SpeedMappingOnl...US®ion=B I have searched the web and come
    close but nothing does it right I would be extremely greatful if anyone could help! Thanks ....
  11. Windows Login Credentials
    (0)
    On an intranet I'm running php. apache and mysql. We use Windows logins. I have a form that
    users can submit. This will require a login. I would prefer to use the windows login and passwords.
    can I link to the windows authentication? The issue I see is when a user changes the password. My
    link would need to update the password....
  12. Is This A Good Script?
    A login script (9)
    Okay, I am trying to password one page of my website. I need confirmation if this is a safe code or
    not. The whole code is on the page I'm protecting. CODE <?php
    include('header.php') ?> <?php // Define your username and password
    $username = "THE_USERNAME"; $password = "THE_PASSWORD"; if
    ($_POST['txtUsername'] != $username ||
    $_POST['txtPassword'] != $password) { ?>
    <h1>Login</h1> <form name="form" method=&....
  13. Phpmyadmin Login Problem!
    (1)
    I have easyphp. But i can not log when i go to phpmyadmin. I directly enter the page. But i think i
    should normally have to log in before enter that page. What should i do to configure the access to
    phpmyadmin? Thank for help....
  14. Login System Help...
    (3)
    I know, nol tried to use this script.. and erm.. i think failed.. but i installed it all good, works
    fine, UNTILL........ i tried to add a new option to the registration. Thhis is the url to the
    site.. where the dl is.. http://evolt.org/PHP-Login-System-with-Adm...nts_per_page=50 I would
    really appreciate it if somebody could add, in all the php files properly a new registration thing,
    called "name" where they write their name in so i know it for future reference. if somebody could
    get that to me i would really appreciate it. ALSO... if you can't do that, or jus....
  15. <?php ?> Sloppy Login Script
    Sloppy login script, couse i used @ on one string (12)
    Here's a sloppy 3 files login script. First file is Login file that looks like this login.php
    CODE <form action="check.php" method="post"> Username: <input
    type="text" name="username1"><br /> Password: <input
    type="password" name="password1"><br /> <input type="submit"
    value="Login"> </form> Basicly that is HTML form that's used for input
    Second part of the script is the check.php that we call from our login.php form QUOTE ....
  16. New Arisen Site Problem
    Nettek Login Trouble (2)
    Okay, so I installed Nettek and got everything set up. But every time I try and login, it says
    it's incorrect. I've gone into the Database and gotten the password and since it was
    simple, put them in and had tried but with no success. I tried changing the password but I still
    couldn't get in. I tried adding another login and it still didn't work. I have no idea
    what's wrong and I need some help.....
  17. What Does This Do?
    $ban = ($data->login) ? $lban : $iban; (4)
    I'm correcting a 'few' php-files for a friend, but I got this line of code: CODE
    $ban = ($data->login) ? $lban : $iban; and I don't know
    what it does xD Could someone please explain me what this line does? Thanks....
  18. Directing To A User To Specific Page First Time Only
    (3)
    I make a signup page. I want that when someone signups and login for first time..then he is directed
    to the page X automatically. but after that it is redirected to Page Y always... Its pretty simple
    thing to do but im not understanding how i can make the script to identify that a person has logged
    in for first time ... anyhelp would be so welcome. Thanks.....
  19. Help Improving My Login Script Code
    The code works okay...just not the authorization part (4)
    I have developed a piece of code /smile.gif" style="vertical-align:middle" emoid=":)" border="0"
    alt="smile.gif" /> that is going to work as my login script for my website. I need some help making
    improvements and creating additional features. Here is my code: CODE <?php
    session_start(); $name = $_POST['username']; $password =
    $_POST['password']; $con =
    mysql_connect("localhost","myDbUser","myDbPassword");
        if(!$con)         {         die(&#....
  20. .htaccess-style Login System And Php
    (13)
    I am trying to make a login system that looks and works like .htaccess using sessions, with a PHP
    script that detects the username used. Let's say I log on with the username "Amezis" and
    correct password. Then I want a PHP (or any other kind of script) to create a cookie or session
    which stores the user name, so it could be possible to store it in a variable as long as the user is
    logged in, and so it can be printed when needed. Basically, this is what I want the script to do:
    CODE /* This file can only be executed after logging in with the htaccess-style login....
  21. Login Script
    (11)
    I am using the following code as a login page. I try to start by checking if a session already
    exists so that people don't have to login each time. The problem is that it is just being
    ignored. How do I check if a session is already set? CODE if
    (isset($_SESSION['loginname'])) { print('you were
    already logged in'); } else { if (submit) {
      list($users,$passwords,$accounttypes)=GetCurrentUsers($user,$
    password,$accounttype);   $nologin=1;   for (&....
  22. Sessions And Login
    Without Cookies (5)
    Hi, I have a login script i made using PHP sessions and MySQL. It works fine but there is a
    problem. As you know Sessions are stored in Cookies by PHP. So if someone has switched Cookies off
    then no sessions will work. How to solve this problem ? Please help me. Thanks and have a good
    day. ....
  23. Automatic Login Using Curl
    (1)
    If you'are lazy people like me. This script may help u. This script is to automated our login to
    some site. You must have cURL installed to use this script. CODE <?php // INIT CURL
    $ch = curl_init(); // SET URL FOR THE POST FORM LOGIN curl_setopt($ch,
    CURLOPT_URL, 'http://www.external-site.com/Members/Login.php'); // ENABLE HTTP POST
    curl_setopt ($ch, CURLOPT_POST, 1); // SET POST PARAMETERS : FORM VALUES FOR EACH
    FIELD curl_setopt ($ch, CURLOPT_POSTFIELDS, 'fieldname1=fieldvalue1&fieldna....
  24. Script That Tracks The User Status
    how can I track on or offline users? (4)
    long explaination: hey, I'm building a user profile site right now. And, I kinda know how to
    make a online/offline detector, but not totally sure. I know I can make a mysql database to track
    them, but how does it entrer the information? I could easily put in a field where when they login it
    sets them to online, but if they don't sign out, and just exit the browser, how can I tell.
    short: I want someone to tell me how to make a online/offline status detector, like they have here
    on trap17. I'd be thrilled if you can post to this, thanks, arcticsnpr....
  25. Password Strength / User Availablity Scripts ?
    (2)
    many of u guys would already have noticed that now a days , on most of the websites , when some one
    sign in...as he puts his desired username in the textbox , the page shows that it is not available
    or it is available...without pushing any button etc.. and the second thing , when some one writes
    the password , on the same screen , it shows that password is weak or password is strong... i think
    it is done with php ... can some one give me a link to any page where i can access these scripts ?
    or some one can help me regarding this ?? Thanks for helping always.....
  26. "grand" Login System?
    To Forums, Chat, and Site (7)
    Major problem I got here. A site at which I am employed as PHP Coder (privet-drive.com), needs a
    "grand" login. The login needs to be able to login them into the forums, the chat, and the site, all
    at the SAME time! The big problem is, the chat is located on another website (potterchat.net),
    and the forums are IPB and not PHPBB. Any comments, or some tips on how I am to go about this? I am
    freaking out, and any advice at all would be nice! Thanks in advanced!....
  27. Login / Authetication System Using Database
    adding information (4)
    Is there any way to make such database where I can write like name and passwords.. Then make an
    login box, and when somebody trys to acces the login he needs to write the name and password.. Then
    it is verifyed if is there such name and password and if it is then acces the page.. I think there
    is posible something like that with MySQL (db).. but can anybody say me a script or way to make
    something like that? Alredy thanks......
  28. Customizing Login Script
    please help anyone good in PHP.. (8)
    On my main site i have this login box: Click here! ... And I want to change the look of it in
    new adress but I have only things can be get from that adres.. I put that login box (new) in my
    website and changed the page that opens but when you try to login with wrong username or password..
    It opens like you typed right usernam and password.. So I need to make it work like normal login
    box but there can be logen in with only right username and password.. I think there need to be added
    some cookies or something to that page.. So someone who is good in PHP please help....
  29. Cleaning User Input With Regex
    (1)
    As anyone who works with user input knows, not everyone who submits information makes it look
    proper. One one of my web forms, I parse all the needed fields that I wish to be title cased; their
    name, address, city, etc. I use this to perform this action, which works nicely: PHP Code: CODE
    $string = ucwords(strtolower($string)); This fixes the input if
    the user types in all caps or all lowercase. There is a problem I have been noticing about those who
    enter a generation after their name, such as "Bill Warner III". The above functio....
  30. Problem With A Login Script
    (10)
    I'm making a simple login script, but it doesn't seem to work like I want. The error
    messages work, but if I actually write the right username and password, it won't work.
    Here's the message I get: QUOTE Warning : Cannot modify header information - headers
    already sent by (output started at /home/ngnorge/public_html/fsm/index.php:7) in
    /home/ngnorge/public_html/fsm/logginn/login.php on line 9 Here's the script: login.php
    CODE <?php include('config.php'); if($action ==
    "submit"){ if((....

    1. Looking for user, login, system, setcookies

Searching Video's for user, login, system, setcookies
advertisement



User Login System With Setcookies



 

 

 

 

ADD REPLY / Got an Opinion! Remove these ADs! RAPID SEARCH! Free Web Hosting [X]
Express your Opinions, Thoughts or Contribute more info. to help others.
Ask your Doubts & Queries to get answers, So that "Together We can help others!"
Register FREE for AD-FREE forum, Create your own topics, Ask Questions, track topics, setup subscriptions & notifications and Get a Free Website w/ Email and FTP.
500MB Space *No Ads*, CPanel, FTP, PHP, MySQL, EMails - 100% FREE