Jump to content
floridaflatlander

PHP Illegal string offset, this is so simple it should work

Recommended Posts

I keep getting Illegal string offset 'https_home' and I don't know why.
I've googled but really can't understand what I'm doing wrong, to me this is so straight forward it should work.
I have my local set up with ini_set('display_errors', 1) & error_reporting(E_ALL) but but nothing is displayed on local
Online the error is entered in my log but the link works like a charm on the pages where it is echoed

 

Local php 5.6.30
Online php 5.5 where the issues seem to be
 

if ($local) ) {
$site_info['https_home'] = 'http://localhost/';
} else {
$site_info['https_home'] = 'https://www.mywebsite.com';
}
/* so $site_info['https_home'] is always defined and as I understand it,
the key is 'https_home' and the value is https://www.mywebsite.com.
So both key and value always exist as strings */

<h4 class="text-center"><?php echo '<a href="'.$site_info['https_home'].'/?action=register">Join us!</a>'; ?></h4>

Share this post


Link to post
Share on other sites

What line is giving you this error?  I don't see anything that would involve a 'string offset'.

 

Also - the query string you are appending doesn't need the /.

 

And the h4 tag is inside your php mode so that is a problem all by itself.  Good reason to NOT mix html and php code together, as is highly recommended here.

Share this post


Link to post
Share on other sites

Is this your actual code or just a vague reproduction of your code? Because in the snippet above, the if statements aren't even executed. They're happening in HTML mode.

 

In any case, this is nonsensical, because you're trying to reinvent the wheel. Both the browser and the webserver know the current protocol and host. There's no need to tell them. Either use a relative URL and have the browser resolve it:

/?action=register

(yes, that's a URL)

 

Or make use of the $_SERVER variables to get the protocol and host.

Share this post


Link to post
Share on other sites

The error probably means that $site_info is a string and not an array.

Share this post


Link to post
Share on other sites

So early in the day and Requinex and taught me something new already.

 

I always thought that PHP would alter the type of a var when it was suddenly used differently.  Apparently that doesn't hold true for all types - specifically an array.

 

 

$site_info = 'abcdef';
echo "site_info is string<br><pre>",print_r($site_info,true),"</pre>";

$site_info = 123;
echo "using site_info as integer<br><pre>",print_r($site_info,true),"</pre>";
 
$site_info['idx'] = 'new element';
echo "using site_info as array<br><pre>",print_r($site_info,true),"</pre>";

Share this post


Link to post
Share on other sites

The h4 is the line the the error reports

I took some things out for brevity ie
$site_info['local'] = FALSE; //  or true
$site_info['home'] = 'http://mywebsite.com';
$site_info['debug'] = FALSE; // its true on local
$site_info['ip_admin'] = '**.**.**';

$site_info['css_path'] = http://www.mywebsite.com

 

and yes the if statement is in the html

and I am using /subfolder/index.php?action=.... . Not /?action=register

 

Also I use this for my css path

if on the /index page it uses $site_info['css_path'] = http://www.mywebsite.com

and it doesn't use https path

or

if in /subfolder/member files or register in in this case it uses $site_info['css_path'] = https://www.mywebsite.com

Edited by floridaflatlander

Share this post


Link to post
Share on other sites

Don't understand your last post and disagree with some of it (you ARE Definitely using "/?action=register")

 

Why not show us a more complete example if you haven't solved this string/array conflict yet, in order to eliminate the php/html problem and to clarify how you are setting some vars?

Share this post


Link to post
Share on other sites

The actual path is on the actual php

 

<h4 class="text-center"><?php  echo '<a href="'.$site_info['https_home'].'/ab-member/index.php?action=register">Join us!</a>'; ?></h4>

 

also talking about the things taken out that Guru referred to as a vague reproduction were stuff like

if($local) {

$site_info['local'] = TRUE; //  or true
$site_info['home'] = 'http://localhose/subfolder';
$site_info['debug'] = TRUE; // its true on local
$site_info['ip_admin'] = '**.**.**';

} else {

$site_info['local'] = FALSE; //  or true
$site_info['home'] = 'http://mywebsite';

$site_info['https_home'] = 'https://www.mywebsite.com';

$site_info['debug'] = FALSE; // its true on local
$site_info['ip_admin'] = '**.**.**';

}

Share this post


Link to post
Share on other sites

So - have you solved the problem with this post?

Share this post


Link to post
Share on other sites

Then how about posting a complete set of the relevant code so that it makes sense to us, rather than the previous posts?  Not the whole script, but at least show us code that makes sense, such as having your php and html clearly written as being in php mode or not?

Share this post


Link to post
Share on other sites
// In config file
if($local) {
/* if it's local */
$site_info['home'] = 'http://localhost/subfolder';
$site_info['https_home'] = 'http://localhost/subfolder';
$site_info['debug'] = TRUE; // its true on local
$site_info['ip_admin'] = '**.**.**';
$site_info['https_home'] = 'http://localhost/';
} else {
/* when online array values are */
$site_info['home'] = 'http://mywebsite'; // used only on index page and left column menu
$site_info['https_home'] = 'https://www.mywebsite.com'; /* used everywhere else */
$site_info['debug'] = FALSE; // don't debug inline
$site_info['ip_admin'] = '**.**.**';
$site_info['https_home'] = 'https://www.mywebsite.com';
}

The error is in this line at mysite.com/index.php this page is http, that's where the error is coming from

$site_info['https_home'] is on the index page and referrers to the $_GET file in the link below

// In index file
<h4 class="text-center"><?php  echo '<a href="'.$site_info['https_home'].'/ab-member/index.php?action=register">Join us!</a>'; ?></h4>
Edited by floridaflatlander

Share this post


Link to post
Share on other sites

Alas.....

 

Is the block of code in the first section in php mode?  It is not apparent.

 

And how is the block of code in the second related to the first?  We don't know What could be going on in between!

 

How are "config file" (?) and "index file"(?) related?  Are they actually files?  If so what are their names?  Are they calling each other or are they included in the same file of some other name?

 

You are not making this easy!

Share this post


Link to post
Share on other sites

Alas.....

 

Is the block of code in the first section in php mode?  It is not apparent.

 

And how is the block of code in the second related to the first?  We don't know What could be going on in between!

 

How are "config file" (?) and "index file"(?) related?  Are they actually files?  If so what are their names?  Are they calling each other or are they included in the same file of some other name?

 

You are not making this easy!

 

1. Yes the block of code in the first section is inside php tags it is in the config.inc.php file

2. The second block is in the /index.php which includes config.inc.php

3. config.inc.php is included at the beginning of /index.php. directly.

4. Sorry, I'm trying

 

 

 

Also as a note, my error_log shows NO errors.

I'm getting error notifications by email from this function in the config.inc.php file which is included at the top of every page

function my_error_handler($e_number, $e_message, $e_file, $e_line, $e_vars) {

	global $debug, $webmaster_email;

	$user_ip = strip_tags($_SERVER['REMOTE_ADDR']);

	if($debug) {$br = '<br />';} else {$br = false;}

	// Build the error message.
	$message = "Coding error message \nSub Dictionary: ".htmlentities(dirname($_SERVER['PHP_SELF']), ENT_QUOTES)."";
	$message .= "\nfile: ".htmlentities(basename($_SERVER['PHP_SELF']), ENT_QUOTES)."";
	$message .= "\npath: ".htmlentities($_SERVER['REQUEST_URI'], ENT_QUOTES)."\n";

	$message .= "\nAn error occurred in script '$e_file' on line $e_line: \n$br $e_message\n$br";
	$message .= "\nFrom an ip at http://whatismyipaddress.com/ip/$user_ip";



date_default_timezone_set ('US/Eastern');

	// Add the date and time.
	$message .= "\nDate/Time: " . date('n-j-Y H:i:s') . "\n";

	if ($debug) { // Show the error and if local
	// added to show a more detailed message when get an error from google bot 140218
	$message = $message .= "$br<pre style=\"background-color: white;\">" . print_r ($e_vars, 1) . "</pre>\n$br";
	// Append $e_vars to the $message.
	//	$message2 = $message .= "$br<pre style=\"background-color: white;\">" . print_r ($e_vars, 1) . "</pre>\n$br";
		echo '<p style="background-color: white; color: black;">' . $message . '</p>';
	} else {

		// Log the error:
		error_log($message, 1, $webmaster_email); // Send email.
		
		// Only print an error message if the error isn't a notice or strict.
		if ( ($e_number != E_NOTICE) && ($e_number < 2048)) {
			$php_db_error = '<p class="error">A system error occurred. We apologize for the inconvenience.</p>';
		}

	} // End of $debug IF.

} // End of my_error_handler()

// Use my error handler:
if (!$local) {
set_error_handler ('my_error_handler');
}

Share this post


Link to post
Share on other sites

At this stage of your development process, why do you need your own custom error handler?

 

Show us how and WHERE you turn on error handling?

Share this post


Link to post
Share on other sites

At this stage of your development process, why do you need your own custom error handler?

 

Show us how and WHERE you turn on error handling?

1. It lets me know if something is wrong by email

2. my_error_handler() is always on, it prints info locally and emails errors when online from error_log($message, 1, $webmaster_email); // Send email.

 

 

As a note I went here https://stackoverflow.com/questions/15361392/how-do-i-correct-this-illegal-string-offset and used var_dump($site_info) like they instructed and got this.

for local but all that changes online is the string aka paths

 

array(20) {["home"]=> string(31) "http://localhost/ab-01.0102" ["https_home"]=> string(31) "http://localhost/ab-01.0102" ["debug"]=> bool(true) ["ip_admin"]=> string(9) "127.0.0.1"};

 

And I used print_r($site_info) and got this

Array ([home] => http://localhost/ab-01.0102 [https_home] => http://localhost/ab-01.0102 [folder] => /ab-01.0102 [debug] => 1 [ip_admin] => 127.0.0.1 )

 

Edited by floridaflatlander

Share this post


Link to post
Share on other sites

also this h4 code is in a function join_us_non_mem ($site_info) {}

 

so index.php includes config.php at top then it uses the function above is echo info. the <h4>

Share this post


Link to post
Share on other sites

Where did you place your var_dump at?

 

Where is join_us_non_mem called from and how is it called? What parameter are you passing into it?

Share this post


Link to post
Share on other sites

Thank God you guys don't know me, you'd drive to my house and beat the .. out of me.

I used the function mentioned above in two places, one for mobile and one for other. In the function used for mobile I used an array value, function($site_info['home']) as the input instead of the array causing the string offset.

Sorry for the oversight and thank you for your input.

Once again, sorry.

Share this post


Link to post
Share on other sites

The error probably means that $site_info is a string and not an array.

Yep, and maybe it was my fault googling but I find couldn't anything as simple as that answer.

If I would have looked at the two places I used this function instead of just one this post would have never happened.

Edited by floridaflatlander

Share this post


Link to post
Share on other sites

Pro tip: Look at the stack trace, not just the error message.

 

The code is still nonsensical, but now it's working nonsensical code. Hooray!

Share this post


Link to post
Share on other sites

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.