Home  |  About  | Last |  Submit  |  Contact
AllQuests.com




Previous Question:  Newbie MySQL PHP insert question  PHPNext Question:  help with strpos  PHP
Question Login Sessions ( SitePoint Forums PHP )
Updated: 2008-11-23 03:25:03 (12)
Login Sessions

I'm constantly being redirect back to the login part of this script, though I know the username and password are correct, as is the database connection, etc.

What's wrong?!

PHP Code:

$username = $_POST['username'];

$salt = "stophacking";
$password = md5 ($salt . trim ($_POST['password']));
      
      if (
session_is_registered("$username")){
                       print
'<table cellpadding="0" cellspacing="3" border="0">
                    <tr>
                    <td>Hi <b>'
.$username.'</b>!</td>
                    </tr>
                    </table>
                    '
;
      } else {
    
          
$query = mysql_query("SELECT * FROM kokonut_members WHERE username='$username' AND password='$password'");
          
$set = mysql_fetch_array($query);
          
          if (
mysql_num_rows($query)){
          
               print
'<table cellpadding="0" cellspacing="3" border="0">
                    <tr>
                    <td>Hi <b>'
.$username.'</b>!</td>
                    </tr>
                    </table>
                    '
;
                    
            
$sessionvar = $username;
            
session_register($sessionvar);
               
               } else {
                          print
'
                        <form action="'
.$PHP_SELF.'" method="post" name="submit">
                        <table cellpadding="0" cellspacing="3" border="0">
                        <tr>
                        <td><img src="images/username.gif" width="36" height="15" alt="" border="0" /></td>
                        <td><input type="text" name="username"></td>
                        <td><img src="images/password.gif" width="35" height="15" alt="" border="0" /></td>
                        <td><input type="password" name="password"></td>
                        <td><input type="image" style="padding-left:7px;" name="submit" src="images/login.gif" width="46" height="19"></td>
                        </tr>
                        </table>
                        </form>
                        '
;
               }    
       }

Answers: Login Sessions ( SitePoint Forums PHP )
Login Sessions

session_start() on top of the page.

masfenix

Login Sessions

Have that....

iamkoa

Login Sessions

This is weird. I got things to work in IE, kind of. Upon logging in, I get a correct welcome message, "Hi [username]!". But then as soon as I visit another page (with the same header) I get "Hi !".

How do I read the information set in the session in order to call upon the actual username for every page? It seems as though right now things work fine only because upon logging in for the first time, the $username is being grabbed by the form entry. As soon as the form is no longer filled out, $username is no longer posted and therefore is lost, even though the session is still registered.

How do I fix this?

PHP Code:

$username = addslashes($_POST['username']);

      
$salt = "stophacking";
      
$password = md5($_POST['password']);
      
//$password = md5($salt.trim($_POST['password']));
    
    
if(session_is_registered('username')){
                       print
'<table cellpadding="0" cellspacing="3" border="0">
                    <tr>
                    <td>Welcome <b>'
.$username.'</b>!</td>
                    </tr>
                    </table>
                    '
;
      } else {
    
          
$query = mysql_query("SELECT * FROM kokonut_members WHERE username='$username' AND password='$password'");
          
//$set = mysql_fetch_array($query);
          
          
if (mysql_num_rows($query)){
          while(
$row = mysql_fetch_array($query)){
               print
'<table cellpadding="0" cellspacing="3" border="0">
                    <tr>
                    <td>Hi <b>'
.$username.'</b>!</td>
                    </tr>
                    </table>
                    '
;
                    
            
session_register('username');
               }
               } else {
                          print
'
                        <form action="'
.$PHP_SELF.'" method="post" name="submit">
                        <table cellpadding="0" cellspacing="3" border="0">
                        <tr>
                        <td><img src="images/username.gif" width="36" height="15" alt="" border="0" /></td>
                        <td><input type="text" name="username"></td>
                        <td><img src="images/password.gif" width="35" height="15" alt="" border="0" /></td>
                        <td><input type="password" name="password"></td>
                        <td><input type="image" style="padding-left:7px;" name="submit" src="images/login.gif" width="46" height="19"></td>
                        </tr>
                        </table>
                        </form>
                        '
;
               }
       }
Thanks!

Oh yes, and things seem not to work in Firefox even though I've cleared my cache and cookies about a thousand times. Is it my machine or are things really different in Firefox and should be coded differently?

iamkoa

Login Sessions

Do you have session_start() at the top of every page? If not, you need to.

markl999

Login Sessions

Yes I do...

Another problem. In order to store the username of a person, I'm using cookies. Now I get this error: Warning: Cannot modify header information - headers already sent by (output started at /home/u3/kmetter/html/kokonut_awards/header.php:9) in /home/u3/kmetter/html/kokonut_awards/header.php on line 125

PHP Code:

$username = addslashes($_POST['username']);

      
$salt = "stophacking";
      
$password = md5($_POST['password']);
      
//$password = md5($salt.trim($_POST['password']));
    
    
if(session_is_registered('username')){
    
         
// set cookie
        
if (!isset($_COOKIE["username"])){
             
setcookie("username", $username, time()+604800); /* Expires in a week */
        
}
                       
$username = $HTTP_COOKIE_VARS["username"];
                       print
'<table cellpadding="0" cellspacing="3" border="0">
                    <tr>
                    <td>Welcome <b>'
.$username.'</b>!</td>
                    </tr>
                    </table>
                    '
;
      } else {
    
          
$query = mysql_query("SELECT * FROM kokonut_members WHERE username='$username' AND password='$password'");
          
//$set = mysql_fetch_array($query);
          
          
if (mysql_num_rows($query)){
          
              
//set cookie
              
setcookie("username", $username, time()+604800); /* Expires in a week */
              
$username = $HTTP_COOKIE_VARS["username"];
             print
'<table cellpadding="0" cellspacing="3" border="0">
                    <tr>
                    <td>Hi <b>'
.$username.'</b>!</td>
                    </tr>
                    </table>
                    '
;
                    
            
session_register('username');
               } else {
                          print
'
                        <form action="'
.$PHP_SELF.'" method="post" name="submit">
                        <table cellpadding="0" cellspacing="3" border="0">
                        <tr>
                        <td><img src="images/username.gif" width="36" height="15" alt="" border="0" /></td>
                        <td><input type="text" name="username"></td>
                        <td><img src="images/password.gif" width="35" height="15" alt="" border="0" /></td>
                        <td><input type="password" name="password"></td>
                        <td><input type="image" style="padding-left:7px;" name="submit" src="images/login.gif" width="46" height="19"></td>
                        </tr>
                        </table>
                        </form>
                        '
;
               }
       }
I get the above error for both lines 125 and 108... these are both lines containing this: setcookie("username", $username, time()+604800); /* Expires in a week */

What's wrong?

iamkoa

Login Sessions

use

ob_start() if you are getting this error

"headers already sent by (output started at /home/u3/kmetter/html/kokonut_awards/header.php:9) in /home/u3/kmetter/html/kokonut_awards/header.php on line 125
"

arunkumar

Login Sessions

Now upon logging in things work until I access another page... then I'm taken back to the loggin form. It's a circle.

iamkoa

Login Sessions

GOT IT!

PHP Code:

$username = addslashes($_POST['username']);

      
$salt = "stophacking";
      
$password = md5($_POST['password']);
      
//$password = md5($salt.trim($_POST['password']));
    
session_start();
     if(
session_is_registered('username')){
    
         
// set cookie
        
if (!isset($_COOKIE["username"])){
             
setcookie("username", $username, time()+604800); /* Expires in a week */
        
}
                       
$username = $HTTP_COOKIE_VARS["username"];
                       print
'<table cellpadding="0" cellspacing="3" border="0">
                    <tr>
                    <td>Welcome <b>'
.$username.'</b>!</td>
                    </tr>
                    </table>
                    '
;
      } else {
    
          
$query = mysql_query("SELECT * FROM kokonut_members WHERE username='$username' AND password='$password'");
          
//$set = mysql_fetch_array($query);
          
          
if (mysql_num_rows($query)){
          
              
//set cookie
            
setcookie("username", $username, time()+604800); /* Expires in a week */
              
$username = $HTTP_COOKIE_VARS["username"];
             print
'<table cellpadding="0" cellspacing="3" border="0">
                    <tr>
                    <td>Welcome <b>'
.$username.'</b>!</td>
                    </tr>
                    </table>
                    '
;
                    
            
session_register('username');
               } else {
                          print
'
                        <form action="'
.$PHP_SELF.'" method="post" name="submit">
                        <table cellpadding="0" cellspacing="3" border="0">
                        <tr>
                        <td><img src="images/username.gif" width="36" height="15" alt="" border="0" /></td>
                        <td><input type="text" name="username"></td>
                        <td><img src="images/password.gif" width="35" height="15" alt="" border="0" /></td>
                        <td><input type="password" name="password"></td>
                        <td><input type="image" style="padding-left:7px;" name="submit" src="images/login.gif" width="46" height="19"></td>
                        </tr>
                        </table>
                        </form>
                        '
;
               }
       }
And include " ob_start()" on the top of the page.

Thanks for the help guys!

iamkoa

Login Sessions

You can't set cookies when you've already printed to the screen. (unless you buffer) You must be printing somewhere or have another error before that.

I can't see any major problem with the session code, double check your config, make sure you have:
session.save_handler = files
session.use_cookies = 1
session.name = PHPSESSID

Some of your auth code is not as I would do it exactly, so I've made a few ammendments for you to try out:

PHP Code:

 <? 


$username
= $_POST['username'];
$salt = "stophacking";
$password = md5 ($salt . trim ($_POST['password']));
      
      if (
$_SESSION["username"]){
                    print
'<table cellpadding="0" cellspacing="3" border="0">
                    <tr>
                    <td>Hi <b>'
.$username.'</b>!</td>
                    </tr>
                    </table>
                    '
;
      } else {
    
          
$query = mysql_query("SELECT * FROM kokonut_members WHERE username='$username' AND password='$password'");
          
$set = mysql_fetch_array($query);
          
          if (
mysql_num_rows($query)){
          
               print
'<table cellpadding="0" cellspacing="3" border="0">
                    <tr>
                    <td>Hi <b>'
.$username.'</b>!</td>
                    </tr>
                    </table>
                    '
;
                    
            
$_SESSION["username"] = $username;
               
               } else {
                          print
'
                        <form action="'
.$PHP_SELF.'" method="post" name="submit">
                        <table cellpadding="0" cellspacing="3" border="0">
                        <tr>
                        <td><img src="images/username.gif" width="36" height="15" alt="" border="0" /></td>
                        <td><input type="text" name="username"></td>
                        <td><img src="images/password.gif" width="35" height="15" alt="" border="0" /></td>
                        <td><input type="password" name="password"></td>
                        <td><input type="image" style="padding-left:7px;" name="submit" src="images/login.gif" width="46" height="19"></td>
                        </tr>
                        </table>
                        </form>
                        '
;
               }    
}
       
       
        
?>

Bob Carologees

Login Sessions

oho try iy just put ob_start(); at the top of the page and do it

arunkumar

Login Sessions

Solved guys... see above.

I had to not only add "ob_start();" at the top of the page, but then include "session_start();" before running the sessions. Works like a charm!

Thanks for all the help!

iamkoa

Login Sessions

session_start() was the first thing someone said and you said you have it :|

Bob Carologees

Previous Question:  Newbie MySQL PHP insert question  SitePoint Forums  PHPNext Question:  help with strpos  SitePoint Forums  PHP

- Source: Login Sessions SitePoint Forums PHP
- Previous Question: Newbie MySQL PHP insert question SitePoint Forums PHP
- Next Question: help with strpos SitePoint Forums PHP