Offset problem

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Ree
Forum Regular
Posts: 592
Joined: Fri Jun 10, 2005 1:43 am
Location: LT

Offset problem

Post 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.
Last edited by Ree on Thu Oct 20, 2005 6:26 am, edited 1 time in total.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

it would appear it believes $msg is a string.
Ree
Forum Regular
Posts: 592
Joined: Fri Jun 10, 2005 1:43 am
Location: LT

Post 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?
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Post 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..
Ree
Forum Regular
Posts: 592
Joined: Fri Jun 10, 2005 1:43 am
Location: LT

Post by Ree »

It does absolutely nothing, prints nothing.
[]InTeR[]
Forum Regular
Posts: 416
Joined: Thu Apr 24, 2003 6:51 am
Location: The Netherlands

Post 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?
Ree
Forum Regular
Posts: 592
Joined: Fri Jun 10, 2005 1:43 am
Location: LT

Post 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...
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

var_dump($msg)

I'll bet you see

Code: Select all

String(0) ""
or similar
Ree
Forum Regular
Posts: 592
Joined: Fri Jun 10, 2005 1:43 am
Location: LT

Post 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.
Last edited by Ree on Thu Oct 20, 2005 11:52 am, edited 1 time in total.
Ree
Forum Regular
Posts: 592
Joined: Fri Jun 10, 2005 1:43 am
Location: LT

Post 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?
djot
Forum Contributor
Posts: 313
Joined: Wed Jan 14, 2004 10:21 am
Location: planet earth
Contact:

Post 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
-
Post Reply