Page 1 of 1

Offset problem

Posted: Thu Oct 20, 2005 1:01 am
by Ree
I have problems with my client's host...

back_EN.php:

Code: Select all

//class.Database.php
$msg['NoDB'] = 'Unable to connect to database.';

//class.Authorization.php
$msg['InvalidLogin'] = 'Invalid username or password.';

//class.Validator.php
$msg['EmptyField'] = '<b>{field}</b> field is empty.';
$msg['InvalidField'] = '<b>{field}</b> field is invalid.';
/* ... */
index.php

Code: Select all

include(languages . '/back_EN.php'); //The file above, it includes fine, no errors

//Class includes

/* ... */

if ($authorization->error()) 
{
  $_SESSION['msg'] = $msg[$authorization->getError()]; //Error is here, $authorization->getError() IS 'InvalidLogin', I echoed it
  include('pages/index.php');
  exit;
}
I get Notice: Uninitialized string offset: 0 in ... . Can't I use $msg[$authorization->getError()]?? It works fine on my local machine.

---EDIT---
Sorry, this doesn't work, I accidently checked that on my local machine, so it doesn't apply to this host, it still doesn't work, even if pasted in index.php.
The interesting thing is, that if I delete $msg['InvalidLogin'] = 'Invalid username or password.'; from the back_EN.php and paste it into index.php, then I get no error and it prints the message!
---EDIT---

I'm really not sure what is wrong here. Something unbelievable. Any info greatly appreciated.

Posted: Thu Oct 20, 2005 1:24 am
by feyd
it would appear it believes $msg is a string.

Posted: Thu Oct 20, 2005 1:34 am
by Ree
I have added $msg = array(); (I guess that's what you meant) at the top of the back_EN.php - no help. Damn, I'm not sure what to do, these kind of problems <span style='color:blue' title='I'm naughty, are you naughty?'>smurf</span> me off so much! The host is running 4.3.11, could there be some prob here?

Posted: Thu Oct 20, 2005 5:01 am
by onion2k
What happens if you do print_r($msg); immediately before your "if ($authorization->error())" block? It looks to me like something has overwritten $msg..

Posted: Thu Oct 20, 2005 6:01 am
by Ree
It does absolutely nothing, prints nothing.

Posted: Thu Oct 20, 2005 6:12 am
by []InTeR[]
Looking at your error: Uninitialized string offset: 0 ....

I think he try'ing to do : $_SESSION['msg'] = $msg[0];

So $authorization->getError() return's 0?

Posted: Thu Oct 20, 2005 6:13 am
by Ree
No, as I mentioned above $authorization->getError() IS 'InvalidLogin', I echoed it:

Code: Select all

if ($authorization->error())
{
  echo $authorization->getError(); //Outputs 'InvalidLogin'
  $_SESSION['msg'] = $msg[$authorization->getError()]; //The prob here
  include('pages/index.php');
  exit;
}
I even tried this:

Code: Select all

if ($authorization->error())
{
  $err_id = $authorization->getError();
  echo $err_id; //Outputs 'InvalidLogin'
  $_SESSION['msg'] = $msg[$err_id]; //The prob still there
  include('pages/index.php');
  exit;
}
I have NO clue what is wrong...

Posted: Thu Oct 20, 2005 8:29 am
by feyd
var_dump($msg)

I'll bet you see

Code: Select all

String(0) ""
or similar

Posted: Thu Oct 20, 2005 9:03 am
by Ree
In my first post, the POST of the form goes to the script itself, that is, to index.php

Here's something I tried for testing, pretty much the same situation as in my first post (the POST goes to the script itself).

test.php

Code: Select all

<?php
session_start();
ini_set('display_errors', 1);
error_reporting(E_ALL);
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>
<?php

include('lib/class.Tester.php');
include('lang/back_EN.php');

$_SESSION['msg'] = '';

if (!isset($_POST['sub']))
{
  include('pages/test.php');
  exit;
}

$tester = &new Tester();
$tester->setError('InvalidLogin');

if ($tester->error())
{
  $_SESSION['msg'] = $msg[$tester->getError()];
  include('pages/test.php');
}

?>
</body>
</html>
lang/back_EN.php

Code: Select all

<?php

//class.Database.php
$msg['NoDB'] = 'Unable to connect to database.';

//class.Authorization.php
$msg['InvalidLogin'] = 'Invalid username or password.';

//class.Validator.php
$msg['EmptyField'] = '<b>{field}</b> field is empty.';
$msg['InvalidField'] = '<b>{field}</b> field is invalid.';

/* ... */

?>
lib/class.Tester.php

Code: Select all

<?php

class Tester
{
  var $error;

  function Tester()
  {
    $this->error = '';
  }

  function setError($error)
  {
    $this->error = $error;
  }

  function getError()
  {
    return $this->error;
  }

  function error()
  {
    return (bool)$this->error;
  }
}

?>
pages/test.php

Code: Select all

<br />
<br />
<br />
<?php
echo $_SESSION['msg'];
$_SESSION['msg'] = '';
?>
<br />
<br />
<form method="post" action="test.php">
  <table>
    <tr>
      <td><input type="submit" value="Submit" name="sub" /></td>
    </tr>
  </table>
</form>
On my local machine, I do get the message ('Invalid username or password.'), on the host I get the same uninitialized offset error. Maybe you will be able to spot the problem (if the code has a flaw).

Also, you can check the problem live at http://test.keliozenklai.lt/test2/test.php . It uses exactly the same code as in this post.

Posted: Thu Oct 20, 2005 10:57 am
by Ree
Can you spot something wrong with my test script above? What could be the reason that it doesn't work on the host, but it does on my comp?

Posted: Wed Nov 16, 2005 1:00 pm
by djot
-
How to get rid of that "Notice: Uninitialized string offset: 0"?


I test a string like that, if it is set (later empty) and then "do something";

Code: Select all

if (!isset($para['text'])) {
$para['text']='';
}

if (!empty($para['text'])) {
vardump($para['text']);
do something... e.g. print $para['text'];
}
Now line "do something" throws the error. Normally $para['text'] is a string, but how to circumvent the notice, if it is not set? (vardump shows 0 "" in this case)


I only have this problem with PHP v5.0.5,
not with PHP v5.0.4 or PHP v4.xx

djot
-