Page 1 of 1

session_register()

Posted: Mon Jun 03, 2002 6:42 am
by rats
having a bit of trouble with if(session_register($SESSION_UID)){ which says its "successful" but the $SESSION_UID is not advailable on pages after this one.... ...not sure why..I have removed most of the functions to keep it simple...

Code: Select all

<?php
include"config.inc";
require_once("DB.php");
session_start();
$formuser = $HTTP_POST_VARSї'formuser'];
$formpass = $HTTP_POST_VARSї'formpass'];
$formemail = $HTTP_POST_VARSї'formemail'];

function loginUser($name, $pass)
{
  global $host, $user, $password, $database;
  $epass = encrypt($pass);
  $db = new DB($host, $user, $password, $database);
  //open a connection
  if(!$db->open()){
    die($db->error());
  }
  if(!$db->query("SELECT id FROM user WHERE username = '$name' AND password = '$epass'")){
    die($db->error());
  }
  // if row exists - login/pass is correct
  if ($db->numRows()== 1)
  {
    // register the user's ID
    list($id) = $db->fetchArray();
    session_start();
    $SESSION_UID = $id;
    if(session_register($SESSION_UID)){
      echo ("success");
    }else{
      echo ("failed");
    }
  }
  echo $db->numRows();
  $db->freeResult();
  $db->close();
}

function encrypt($item)
{
  return (md5($item));
}

switch($HTTP_GET_VARSї'act'])
{
  case "login":
    loginUser($formuser, $formpass);
    break;
  case "logout":
    logoutUser();
    break;
  case "new":
    newUser($formname, $formpass, $formemail);
    break;
  case "update":
    updateUser($SESSION_UID);
    break;
  case "view":
    viewUser($formname);
    break;
  case "delete":
    viewUser($formname);
    break;
  default:
    //if they havent specified an action send them to index.php
    header("Location: http://avalon/");
    exit();
}
?>

Posted: Wed Jun 05, 2002 3:39 am
by ozmodiar
You must call session_start() at the top of each page where you want to use the session variable.

Dunno if this is the problem maybe you have been doing this.
What version of PHP are you using?