[php] Password Help

Discussion in 'Web Design & Coding' started by technokid88, Jul 16, 2007.

  1. technokid88

    technokid88 Part of a System Folding Team

    Messages:
    741
    Location:
    In a world without windows
    I'm trying to put a php script to block content on a page, unless you put in your registered user name and password. I have it where if a person comes to this page they get message that says you are not logged in, and a link to log in. This will happen to ever page i put this code in to. What i want is after they get this message and log in i want them to go to the page with the blocked page logged in and want it to be displayed. Any help would be much appreciated. Thanks

    Code:
    <?php
    $username = $_COOKIE['loggedin'];
    if (!isset($_COOKIE['loggedin'])) die("You are not logged in, <a href=login1.php>click here</a> to login.");
    echo "You are logged in $username";
    ?>
    This is the login.php code below.

    Code:
    <?php
    ob_start();
    
    include("config.php"); 
    
    // connect to the mysql server 
    $link = mysql_connect($server, $db_user, $db_pass) 
    or die ("Could not connect to mysql because ".mysql_error()); 
    
    // select the database
    mysql_select_db($database) 
    or die ("Could not select database because ".mysql_error()); 
    
    $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 or password with: <strong>".$_POST['username']."</strong><br>"; 
    echo "<a href=login1.php>Try again</a>"; 
    exit;
    
    } else { 
    
    setcookie("loggedin", "".$_POST['username']."", time()+(3600 * 24));
    setcookie("username", "".$_POST['username']."", "TRUE");
    echo "Welcome: <strong>".$_POST['username']."</strong><br>"; 
    echo "Continue to the <a href=members.php>Listing you were looking at.</a> section.";
    }
    ob_end_flush();
    ?>
     
    Last edited: Jul 17, 2007
  2. Geffy

    Geffy Moderator Folding Team

    Messages:
    7,805
    Location:
    United Kingdom
    You need to use sessions really, you can store an authorisation key in a cookie if you want to remember users across browser closures but sessions are what you need.

    Take a look at SitePoints PHP Sessions tutorial
     
  3. technokid88

    technokid88 Part of a System Folding Team

    Messages:
    741
    Location:
    In a world without windows
    Hey thanks for the info, looks like the exact thing i was looking for.