Jump to content

Had a problem with session,Please help


xux

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.