Jump to content


Photo

Had a problem with session,Please help


  • Please log in to reply
14 replies to this topic

#1 xux

xux
  • Members
  • PipPipPip
  • Advanced Member
  • 85 posts

Posted 29 August 2006 - 03:07 PM

Hi everybody,
                I am trying to implement session control woth a login mechanism.it has being generating an error which i have found difficult to rectify.It has been generating
Warning: session_start(): Cannot send session cache limiter - headers already sent (output started at D:\wwwroot\t\login.php:12) 
in D:\wwwroot\t\login.php on line 236
please i need help.Thanks

#2 Ifa

Ifa
  • Members
  • PipPipPip
  • Advanced Member
  • 88 posts
  • LocationRauma, Finland

Posted 29 August 2006 - 03:11 PM

Try adding the session_start(): to the top of the file, or if you are including the file, at the top of the file that is including it....
I'm just a guy who likes to code for fun...

#3 Orio

Orio
  • Staff Alumni
  • Advanced Member
  • 2,491 posts

Posted 29 August 2006 - 03:12 PM

You cant call session_start(), header(), setcookie() and other fucntions after the script outputs something. session_start() should come in the begining.
Can you post your code so I could show you where you are wrong?

Orio.
Think you're smarty?

(Gone until 20 to November)

#4 xux

xux
  • Members
  • PipPipPip
  • Advanced Member
  • 85 posts

Posted 29 August 2006 - 03:24 PM

Thanks,here is my code
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Login</title>
</head>

<body>
<?php 
									
	   session_start();
	   
	if (isset($HTTP_POST_VARS['user_name'])&&(isset($HTTP_POST_VARS['pwd'])))
	{
    // getting data from the login form
       $user_name = $HTTP_POST_VARS['user_name'];
       $pwd = $HTTP_POST_VARS['pwd'];
$connection = mysql_pconnect('localhost', 'root', 'justus') 
or die ('Unable to connect!');
// select database for use
mysql_select_db('sme_cms') or die ('Unable to select database!');
$query = 'select * from auth '
           ."where name='$user_name' "
           ." and pass=password('$pwd')";
  $result = mysql_query($query);
  if (mysql_num_rows($result) >0 )
  {
    // if they are in the database register the user id
    $HTTP_SESSION_VARS['valid_user'] = $user_name;    
  }
}


if (isset($HTTP_SESSION_VARS['valid_user']))
  {
    echo 'You are logged in as: '.$HTTP_SESSION_VARS['valid_user'].' <br />';
    echo '<a href="logout.php">Log out</a><br />';
  }
  else
  {
    if (isset($user_name))
    {
      // if they've tried and failed to log in
      echo 'Could not log you in';
    }
    else 
    {
      // they have not tried to log in yet or have logged out
      echo 'You are not logged in.<br />';
    }
	  }
     ?>	
</body>
</html>
Thanks 



#5 Ifa

Ifa
  • Members
  • PipPipPip
  • Advanced Member
  • 88 posts
  • LocationRauma, Finland

Posted 29 August 2006 - 03:53 PM

<?PHP session_start(); ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Login</title>
</head>

<body>
<?php 						
	   
	if (isset($HTTP_POST_VARS['user_name'])&&(isset($HTTP_POST_VARS['pwd'])))
	{
    // getting data from the login form
       $user_name = $HTTP_POST_VARS['user_name'];
       $pwd = $HTTP_POST_VARS['pwd'];
$connection = mysql_pconnect('localhost', 'root', 'justus') 
or die ('Unable to connect!');
// select database for use
mysql_select_db('sme_cms') or die ('Unable to select database!');
$query = 'select * from auth '
           ."where name='$user_name' "
           ." and pass=password('$pwd')";
  $result = mysql_query($query);
  if (mysql_num_rows($result) >0 )
  {
    // if they are in the database register the user id
    $HTTP_SESSION_VARS['valid_user'] = $user_name;    
  }
}


if (isset($HTTP_SESSION_VARS['valid_user']))
  {
    echo 'You are logged in as: '.$HTTP_SESSION_VARS['valid_user'].' <br />';
    echo '<a href="logout.php">Log out</a><br />';
  }
  else
  {
    if (isset($user_name))
    {
      // if they've tried and failed to log in
      echo 'Could not log you in';
    }
    else 
    {
      // they have not tried to log in yet or have logged out
      echo 'You are not logged in.<br />';
    }
	  }
     ?>	
</body>
</html>

I'm just a guy who likes to code for fun...

#6 xux

xux
  • Members
  • PipPipPip
  • Advanced Member
  • 85 posts

Posted 29 August 2006 - 04:38 PM

Thanks you to everybody,am going to try that out.I really appreciate everybody.

#7 xux

xux
  • Members
  • PipPipPip
  • Advanced Member
  • 85 posts

Posted 30 August 2006 - 11:39 AM

Hi,
  The code stop giving me errors,but it refuse to log me in,just saying cannot be logged in even when the right user name and password was used.Please what do you think is wrong.Thanks

#8 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 30 August 2006 - 12:28 PM

You are using old super global variables, you should use the new superglobal variables which are
$_POST instead of $HTTP_POST_VARS
$_GET instead of $HTTP_GET_VARS
$_SESSION instead of $HTTP_SESSION_VARS
$_COOKIE instead of $HTTP_COOKIE_VARS

Also this code here:
else
  {
    if (isset($user_name))
    {
      // if they've tried and failed to log in
      echo 'Could not log you in';
    }
    else 
    {
      // they have not tried to log in yet or have logged out
      echo 'You are not logged in.<br />';
    }


Is a little strange. You are checking to see if $user_name exists, and if it does you say you cannot be logged in.

Shouldnt this
if (isset($user_name))

be
if (!isset($user_name))


Otherwise if the user fills in the form correctly and hits submits it always goona say 'Could not log you in'. With the latter code it now checks whether the $user_name var doesnt exist.

#9 xux

xux
  • Members
  • PipPipPip
  • Advanced Member
  • 85 posts

Posted 30 August 2006 - 03:15 PM

Hi,
  Thanks.I will try that out.But am using php4,can I used the new variable?I appreciate it,see ya later.

#10 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 30 August 2006 - 03:19 PM

Yes the new superglobals where introduced in PHP 4.2. You should be able to use the new superglobals in PHP4.2 or greater

#11 xux

xux
  • Members
  • PipPipPip
  • Advanced Member
  • 85 posts

Posted 30 August 2006 - 04:10 PM

Hi,
  After effecting the changes you suggested it is still outputing 'could not log in' despite using the right name and password.what do you think i can do,or is there another way to implement it?
i see that you are a moderator,that shows you must be php guru,can you also help with an updating script into a mysql database using database?thanks

#12 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 30 August 2006 - 04:13 PM

Diod you read this bit of my post above:

Also this code here:
else
  {
    if (isset($user_name))
    {
      // if they've tried and failed to log in
      echo 'Could not log you in';
    }
    else
    {
      // they have not tried to log in yet or have logged out
      echo 'You are not logged in.<br />';
    }

Is a little strange. You are checking to see if $user_name exists, and if it does you say you cannot be logged in.

Shouldnt this
if (isset($user_name))
be
if (!isset($user_name))

Otherwise if the user fills in the form correctly and hits submits it always goona say 'Could not log you in'. With the latter code it now checks whether the $user_name var doesnt exist.



#13 xux

xux
  • Members
  • PipPipPip
  • Advanced Member
  • 85 posts

Posted 30 August 2006 - 04:20 PM

ok,
maybe i should remove that portion of the code.or what do you think?thanks

#14 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 30 August 2006 - 04:34 PM

Swap if (isset($user_name))
to
if (!isset($user_name))

notice the ! before isset

#15 xux

xux
  • Members
  • PipPipPip
  • Advanced Member
  • 85 posts

Posted 31 August 2006 - 03:38 PM

Thanks,
          I tried that,but it is still reporting the same error.I will appreciate ur assistance.Thanks




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users