Jump to content

Archived

This topic is now archived and is closed to further replies.

xux

Had a problem with session,Please help

Recommended Posts

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
[code]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[/code]
please i need help.Thanks

Share this post


Link to post
Share on other sites
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....

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Thanks,here is my code
[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

[/code]

Share this post


Link to post
Share on other sites
[code]<?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>[/code]

Share this post


Link to post
Share on other sites
Thanks you to everybody,am going to try that out.I really appreciate everybody.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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:
[code=php:0]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 />';
    }[/code]


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
[code=php:0]if (isset($user_name))[/code]

be
[code=php:0]if (!isset($user_name))[/code]


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

Share this post


Link to post
Share on other sites
Hi,
  Thanks.I will try that out.But am using php4,can I used the new variable?I appreciate it,see ya later.

Share this post


Link to post
Share on other sites
Yes the new superglobals where introduced in PHP 4.2. You should be able to use the new superglobals in PHP4.2 or greater

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
Diod you read this bit of my post above:
[quote]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.[/quote]

Share this post


Link to post
Share on other sites
ok,
maybe i should remove that portion of the code.or what do you think?thanks

Share this post


Link to post
Share on other sites
Swap if (isset($user_name))
to
if (!isset($user_name))

notice the ! before isset

Share this post


Link to post
Share on other sites
Thanks,
          I tried that,but it is still reporting the same error.I will appreciate ur assistance.Thanks

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.