CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 13 of 13
  1. #1
    Join Date
    May 2009
    Posts
    160

    best authentication method PHP5

    Earlier i would take username and password, match with db and on success redirect

    Code:
    session_register("myusername");
    On every page that is viewed i would include a file that checks if the session is registered or not.

    Now that session_register is deprecated, i did try to google alot to see what would be the best way to authenticate. With the above method i always noticed lots of hacking, session hijacking happening.

    WHAT IS THE BEST AND MOST SECURED AND EFFICIENT WAY TO AUTHENTICATE. IS THERE AN OBJECT ORIENTED WAY OF ACCOMPLISHING THIS?

    thank you

  2. #2
    Join Date
    May 2002
    Posts
    10,943

    Re: best authentication method PHP5

    Always use session_name(), not session_register().
    If the post was helpful...Rate it! Remember to use [code] or [php] tags.

  3. #3
    Join Date
    May 2009
    Posts
    160

    Re: best authentication method PHP5

    Quote Originally Posted by PeejAvery View Post
    Always use session_name(), not session_register().
    should i provide the name?

    Also what should i check for in other pages to make sure un authenticated users donot access those pages?

  4. #4
    Join Date
    May 2002
    Posts
    10,943

    Re: best authentication method PHP5

    Always provide a name...unless you want session stealing and poor security.

    Upon logging in, save the current user to a session variable named user. Then check for that session variable at the beginning of every page.
    If the post was helpful...Rate it! Remember to use [code] or [php] tags.

  5. #5
    Join Date
    May 2009
    Posts
    160

    Re: best authentication method PHP5

    Quote Originally Posted by PeejAvery View Post
    Always provide a name...unless you want session stealing and poor security.

    Upon logging in, save the current user to a session variable named user. Then check for that session variable at the beginning of every page.
    would the below be rite

    Code:
    mysql query results in a match.
    
    $user = data['username'];
    
    $_SESSION['user'] = $user;
    
    session_name($user);
    AND ON EVERY PAGE CHECK BELOW

    Code:
    if(isset(session_name($_SESSION['user'])))
    //good
    else
    //redirect to index.php
    Please correct me if its not the most efficient way.

  6. #6
    Join Date
    May 2002
    Posts
    10,943

    Re: best authentication method PHP5

    It works...but instead of putting the code at the top of every page...create an authentication.php file and require it at the top of every page.
    If the post was helpful...Rate it! Remember to use [code] or [php] tags.

  7. #7
    Join Date
    May 2009
    Posts
    160

    Re: best authentication method PHP5

    Quote Originally Posted by PeejAvery View Post
    It works...but instead of putting the code at the top of every page...create an authentication.php file and require it at the top of every page.
    yea i know it works....... yea i will include it into a file but is this the best way ? i wish to know if there is a better way i can accomplish this.

  8. #8
    Join Date
    May 2002
    Posts
    10,943

    Re: best authentication method PHP5

    Yes. An required header file is the only way to assure security across all the pages.
    If the post was helpful...Rate it! Remember to use [code] or [php] tags.

  9. #9
    Join Date
    May 2009
    Posts
    160

    Re: best authentication method PHP5

    Quote Originally Posted by PeejAvery View Post
    Yes. An required header file is the only way to assure security across all the pages.
    wat do you mean by a required header file is the only way to assure security?

  10. #10
    Join Date
    May 2002
    Posts
    10,943

    Re: best authentication method PHP5

    If you don't require a header file at the top...then how do you expect to restrict it's access? Other than .htaccess...but that excludes any database interaction.
    If the post was helpful...Rate it! Remember to use [code] or [php] tags.

  11. #11
    Join Date
    May 2009
    Posts
    160

    Re: best authentication method PHP5

    Quote Originally Posted by PeejAvery View Post
    If you don't require a header file at the top...then how do you expect to restrict it's access? Other than .htaccess...but that excludes any database interaction.
    gotcha... would session_destroy be the right way on a logout ?

  12. #12
    Join Date
    May 2002
    Posts
    10,943

    Re: best authentication method PHP5

    Always.
    If the post was helpful...Rate it! Remember to use [code] or [php] tags.

  13. #13
    Join Date
    Sep 2010
    Posts
    5

    Re: best authentication method PHP5

    It may be interesting to keep your sessions in a database as well. On a shared server this may resolve security issues with other websites hosted on the same box.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured