View Single Post
  #33  
Old 04-16-2005, 09:16 AM
JonM is offline JonM
Registered User

Join Date: Jun 2004
Posts: 2,156

u know what guys...i'm liking ur code and all, but how can you tell if there logged in? all it does is check if they aren't logged in if i'm readding it correctly...

PHP Code:
<? 
function login_form() {?> 

    <form method="post" action="<? Echo($_SERVER['PHP_SELF']);?>"> 
    Username: <input type="text" name="username" /><br /> 
    Password: <input type="password" name="password" /><br /> 
    <input type="hidden" name="login" value="true" /> 
    <input type="submit" name="login" value=" Login " /> <input type="reset" value=" Reset " /> 
    </form> 

<? 

If(
$_POST['login']) { 

      
$username=mysql_real_escape_string($_POST['userna
me'
]); 
      
$password=mysql_real_escape_string($_POST['passwo
rd'
]); 
     
    
$query=mysql_query("SELECT * FROM users WHERE username='".$username."' AND password='".$password."' LIMIT 1"); 
     
    If(
mysql_num_rows($query) > 0) { 
        
setcookie("loggedin","true",time+36000); //1 day 
        
sleep(1); 
    } 
    
mysql_free_result($query); 
     
    
header("Location: ".$_SERVER['PHP_SELF']); 
    exit; 


If(
$_COOKIE['loggedin']!="true") { 

    Echo(
" You do not have permission to access this page. Please login Below. "); 
    
login_form(); // print the login form 
    
exit; 
}
?>

c i would change the cookie to this to:
PHP Code:
setcookie("loggedin","".$username."",time+36000); //1 day 
then i would replace this part
PHP Code:
If($_COOKIE['loggedin']!="true") { 

    Echo(
" You do not have permission to access this page. Please login Below. "); 
    
login_form(); // print the login form 
    
exit; 
}
?> 
to:
PHP Code:

//replace with if(!$_COOKIE['loggedin'] || $_COOKIE['loggedin']==""){
echo'This is a restricted page to members only, you may login or <a href="register.php">Register</a>';
}else{
/*the users got the cookie and is loggedin*/
$nick=$_COOKIE['loggedin'];
echo
'Welcome back <b>'.$nick.'</b>!  You may now access restricted parts of the site!';

also u may just need to fix some db issues, like your connect or something for the error.
Reply With Quote