I am not so sure where to ask this, I have problem with PHP

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
olschan
Forum Newbie
Posts: 2
Joined: Wed Dec 17, 2008 2:56 am

I am not so sure where to ask this, I have problem with PHP

Post by olschan »

Moderator has separated this from an unrelated thread and moved to an appropriate Forum. Also edited the code segment to use BBcode tags, for readability.

olschan: For any future posting here, please learn how to use the forums properly. Thank you.


HI, I need some help with my login code:

Code: Select all

 
<?
ob_start();
session_start( );
require_once("db_connect.php");
 
//check cookie
 if ($_SESSION['logged_in'] != 1 && isset($_COOKIE['login_cookie'])) {
    list($user, $pass) = explode('[]', $_COOKIE['login_cookie']);
     $qu = mysql_query("SELECT `password` FROM `users` WHERE `username` = '".addslashes($user)."'");
    if (mysql_num_rows($qu) == 1) {
        $passw = mysql_fetch_object($qu);
        if ($passw->user_password == $pass) {
          $_SESSION['logged_in'] = 1;
           $_SESSION['username'] = $user;
            $_SESSION['password'] = $pass;
        }
    }
 }
 
if(!isset($_SESSION['username']) && !isset($_SESSION['password'])) {
   $_SESSION['logged_in'] = 0;
   $user = "Guest"; 
 }
//the end of page header 
?>
 
 
 
<?
 ob_start();
 //Dave says: Turn on output buffering
 
require_once("db_connect.php") ;
 
 
//Dave says: Determine whether a variable is set
if(isset($_SESSION['username']) && isset($_SESSION['password'])) {
       //REDIRECT TO USERS PROFILE...
       header("Location: http://php.davidolsan.com/usersPage.php");
} //end if logged in
 
 
//IF SUBMIT BUTTON PRESSED
if(isset($_POST['submit'])) {
 
   if(!$_POST['username']) die("Error: You must enter your username before logging in.");
   if(!$_POST['password']) die("Error: You must enter your password before logging in.");
   
 //set cookie if checked
   if(!empty($_POST['stay_in'])) {  
         $joined =''.$_POST['username'].'[]'.md5($_POST['password']).'';
         setcookie('login_cookie', $joined, 2147483647, '/', '.www.php.davidolsan.com');   
    } //end if
 
 
//verify user...
$get_user = mysql_query("SELECT * FROM `users` WHERE username = '".$_POST['username']."' 
                                                                          AND
                                                                          
                                                                          password = '".md5($_POST['password'])."'");
$q = mysql_fetch_object($get_user);
    if(!$q) die("Login Failure: An error occured, please verify your username and password are correct.");
 
 
//set session variables 
$_SESSION['logged_in'] = 1;
$_SESSION['username'] = $_POST['username']; 
$_SESSION['password'] = $_POST['password']; 
session_write_close();
 
header("Location: http://www.php.davidolsan.com");
 
 
} else {
//show login form
?>
<form name="login" method="post" action="<? $_SERVER['PHP_SELF']; ?>">
<table>
<tr>
  <td>Username:<input type="text" id="username" name="username"></td>
</tr>
<tr>
  <td>Password:<input type="password" id="password" name="password"></td>
</tr>
<tr>
  <td>Submit: <input type="submit" value="Submit" name="submit" id="submit"></td>
</tr>
<tr>
<td>Remember? <input type="checkbox" name="stay_in[]" checked="yes"></td>
</tr>
</table>
</form>
<?
}//end else
?>
 
I have the user and password in database ok but still I get Login Failure: An error occured, please verify your username and password are correct.
Any IDEA??
I am inexperienced with PHP. :-)
olschan
Forum Newbie
Posts: 2
Joined: Wed Dec 17, 2008 2:56 am

Re: I am not so sure where to ask this, I have problem with PHP

Post by olschan »

ok my login works now i need to fix session
Post Reply