Results 1 to 9 of 9

Thread: cookies help and membership

  1. #1
    Join Date
    Jul 2007
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default cookies help and membership

    i made a software but i need help in making permissions for members to enter certain pages ONLY if they are logged in an account that gives them permission to enter it
    I dont know anything about cookies please can you tell me the basics of show me a simple code to know how stuff work
    Regards

  2. #2
    Join Date
    Jul 2007
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    hey!
    to make a cookie you want to run this. btw the +3600 is time measured in seconds i think.

    Code:
    setcookie("name", "value", time()+3600);
    to expire a cookie
    Code:
    setcookie("name", "value", time()-3600);
    one thing you will need to do is verify that the person has a valid cookie. so once they are logged in you will want to check that their cookie is still active. for example a person did not type in the address of secure pages.

    Code:
     
    //cookie validation
      $valid_check = $_COOKIE['name'];
    if($valid_check == "value")
    {
    	// Login OK
    }else{
    	// Login FAKE
    	header("Location: ../index.php");
    }

  3. #3
    Join Date
    Jul 2007
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    until now everything i agree upon but what about the pages which i have on the server if one have the url he can access easily without the need to login

  4. #4
    Join Date
    Jul 2007
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    anyways do u have a similar login code to see how it works cuz i created 1 i think there is something wrong in it

  5. #5
    Join Date
    Mar 2006
    Location
    Cleveland, Ohio
    Posts
    574
    Thanks
    6
    Thanked 5 Times in 5 Posts

    Default

    here's a REALLY simple login code:

    PHP Code:
    session_start();

    $protected_content 'WHATEVER YOU WANT PASSWORD PROTECTED HERE';

    if(!@
    $_SESSION['auth']){

    $username 'practice';
    $password 'practice';
    $user $_POST['user'];
    $pass $_POST['pass'];

        if(
    $user !== $username || $pass !== $password){ 
        
    header('Location:login.php');}
        else{ 
        
    $_SESSION['auth'] = 'true';
        echo 
    $protected_content;}
    }

    else{
    ob_start();
    header('Location:login.php');
    ob_end_flush();} 
    If you have any questions, feel free to ask. Just replace "practice" with the username and password of your choice. And, if needed, change login.php to the name of the login page.
    Last edited by alexjewell; 07-18-2007 at 01:24 AM.
    Thou com'st in such a questionable shape
    Hamlet, Act 1, Scene 4

  6. #6
    Join Date
    Jul 2007
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by alexjewell View Post
    here's a REALLY simple login code:

    PHP Code:
    session_start();

    $protected_content 'WHATEVER YOU WANT PASSWORD PROTECTED HERE';

    if(!@
    $_SESSION['auth']){

    $username 'practice';
    $password 'practice';
    $user $_POST['user'];
    $pass $_POST['pass'];

        if(
    $user !== $username || $pass !== $password){ 
        
    header('Location:login.php');}
        else{ 
        
    $_SESSION['auth'] = 'true';
        echo 
    $protected_content;}
    }

    else{
    ob_start();
    header('Location:login.php');
    ob_end_flush();} 
    If you have any questions, feel free to ask. Just replace "practice" with the username and password of your choice. And, if needed, change login.php to the name of the login page.
    isnt this script using sessions? i think he wants to use cookies, although sessions are better.

    that reminds me i need to start learning sessions.

  7. #7
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    Yes. It uses sessions. Sessions usually use a cookie to store the session id, which keeps the user connected to the current session.
    However, using cookies alone would be incredibly easy to fake because the values of cookies are directly available to the user.
    Sessions are a much better idea.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  8. #8
    Join Date
    Jul 2007
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    'but now am facing another problem which is checking if the username and password available in mysql database and they both match
    How can i check the replaced username and password with the ones in the database

  9. #9
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    http://php-mysql-tutorial.com

    Search the database to see if it matches.

    $query = "SELECT * FROM 'usertable' WHERE 'password'='$password' AND 'username'='$user';"
    If that returns any results, it's valid, then. In the simplest of scenarios, at least.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

Bookmarks

Posting Permissions

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