OK, I wrote this script following a tutorial and I believe all the coding is correct. Apparently not, because I'm getting the following error:

Fatal error: Cannot unset string offsets in C:\xampp\xampp\php\pear\Auth.php on line 180

Here's the code:

<?php

require_once("c:\\xampp\\xampp\\php\\pear\\Auth\\Auth.php");

$LEGAL_FUNCS = array( "main", "signup", "logout");

$PAGE['AUTH'] = new Auth("File", "/users/data.txt", "write_login");
$PAGE['msg'] = "";

switchboard();

function switchboard() {
  $comms = $GLOBALS['LEGAL_FUNCS'];
  $cmd = $_GET['cmd'];
  if( empty($cmd) || !in_array( $cmd, $comms)) {
    $cmd = $comms[0];
  }
  $page = $cmd();
  if( !empty($page)) {
    @include( $page );
  }
}

function setMessage( $msg ) {
  $GLOBALS['PAGE']['msg'] .= "{$msg}<br />";
}

function authenticate() {
  $auth = $GLOBALS['PAGE']['AUTH'];
  $auth->start();
  if( ! $auth->getAuth()) {
    exit();
  }

  return true;
}

function write_login() {
  $auth = $GLOBALS['PAGE']['AUTH'];
  if( $auth->getStatus() == AUTH_EXPIRED) {
    setMessage("Your session has expired");
  }
  elseif( $auth->getStatus() == AUTH_WRONG_LOGIN) {
    setMessage("Login failed. Try again or sign up");
  }
  include_once("login.php");
}

function signup() {
  if( empty($_GET['username'])) {
    return "signup.php";
  }
  $signup = $GLOBALS['PAGE']['AUTH']->addUser( $_GET['username'], $_GET['password']);
  if( $signup instanceof pear_error) {
    setMessage( $signup->message );
    return "signup.php";
  }
  setMessage("Signup Succesful!");
  return main();
}

function logout() {
  $auth = $GLOBALS['PAGE']['AUTH'];
  authenticate();
  $auth->logout();
  setMessage($auth->getUsername()." logged out");
}

function main() {
  authenticate();
  return "main.php";
}
?>

Can anyone help?

    The thing is, Auth.php is part of the PEAR package, so I assume that since it is a stable release the problem is not with the PEAR package but with my use of it 🙁

    Here's the function in which line 180 is found:

       function Auth($storageDriver, $options = '', $loginFunction = '', $showLogin = true)
        {
            if (!empty($options['sessionName'])) {
                $this->_sessionName = $options['sessionName'];
                unset($options['sessionName']);
            }
    
        if ($loginFunction != '' && is_callable($loginFunction)) {
            $this->loginFunction = $loginFunction;
        }
    
        if (is_bool($showLogin)) {
            $this->showLogin = $showLogin;
        }
    
        if (is_object($storageDriver)) {
            $this->storage =& $storageDriver;
        } else {
            $this->storage = $this->_factory($storageDriver, $options);
        }
        // Pass a reference to auth to the container, ugly but works
        // this is used by the DB container to use method setAuthData not staticaly.
        $this->storage->_auth_obj =& $this;
    }
    

    and here is my use of the function:

    $PAGE['AUTH'] = new Auth("File", "/users/data.txt", "write_login");
    

      Heh... sorry, accidentally deleted my post.

      You said stable release, so I'll assume v1.2.3. Looking at lines 176 - 181:

      function Auth($storageDriver, $options = '', $loginFunction = '', $showLogin = true)
      {
          if (!empty($options['sessionName'])) {
              $this->_sessionName = $options['sessionName'];
              unset($options['sessionName']);  // Line 180
          }
      

      Interesting. It allows options to be a string, but then it went ahead and assumed it was an array anyway. So much for bug free, eh?

        So any way I can fix this? Which string is being interpreted incorrectly (or vice versa)?

          The string being interpreted incorrectly is the options, which is the second parameter, where you have "/users/data.txt".

          Looking through the code, it looks like the File storage method really wants the options to be a string (the filename), exactly as you gave it. The auth class doesn't like options to be a string. So I'm not sure that there is a good workaround without actually modifying the auth class.

          If you're okay with modifying the auth class, then try changing line 178 to this:

          if (is_array($options) && !empty($options['sessionName'])) {

          If it makes you feel any better v1.3 (beta) doesn't appear to have this issue. At least from what I could trace through it.

            Thanks. . .I'll try modifying that.


            I did, and I don't get the error anymore. . .but the stupid localhost server is refusing to load anything so I can't see if the script actually works 🙁🙁🙁🙁

              Write a Reply...