Jump to content

PHP Notice: Undefined offset: 3


Bottyz
Go to solution Solved by Ch0cu3r,

Recommended Posts

$domains = count($alloweddomains);
for($y=0;$y<$domains+1;$y++) {
	if((stristr($_SERVER['HTTP_REFERER'], $alloweddomains[$y]))) {
		$allowed = 1;
	}
} 

Hi all,

 

Probably a really easy one for you to solve, but I'm a little stumped. I've recently upgraded to php 5.4 and have started getting a few php notices and I'm going through them one by one, but I'm stuck fixing the following:

 

[29-Jul-2015 06:54:21 America/New_York] PHP Notice:  Undefined offset: 3 in /public_html/filedownload.php on line 25

 

I've attached the code and I believe its referring to the $y variable.

 

 

 

Any ideas or assistance is always appreciated.

Link to comment
Share on other sites

Oh and I'm also getting a very similar PHP notice for the below, again I think the culprit is the $o variable.

		class unicode_replace_entities {
		    public function UTF8entities($content="") {
		        $contents = $this->unicode_string_to_array($content);
		        $swap = "";
		        $iCount = count($contents);
		        for ($o=0;$o<$iCount;$o++) {
		            $contents[$o] = $this->unicode_entity_replace($contents[$o]);
		           $swap .= $contents[$o];
			    }
			    return mb_convert_encoding($swap,"UTF-8"); //not really necessary, but why not.
			}

This time the notice is: [28-Jul-2015 06:44:41 America/New_York] PHP Notice:  Uninitialized string offset: 0 in /public_html/contactform.php on line 108

 

 

Pulling what little hair I have left out over these!

 

Thanks :)

Link to comment
Share on other sites

 

29-Jul-2015 06:54:21 America/New_York] PHP Notice:  Undefined offset: 3 in /public_html/filedownload.php on line 25

You  are getting that notice because there is not a array index 3 in $alloweddomains. This is because your for loop is most likely iterating one too many times, due to you adding one to $domains in the condition. 

If  $alloweddomains is an array, then use a foreach loop instead

// loop over each allowed domain
foreach($alloweddomains as $domain) {
	if((stristr($_SERVER['HTTP_REFERER'], $domain))) {
		$allowed = 1;
	}
} 

 

This time the notice is: [28-Jul-2015 06:44:41 America/New_York] PHP Notice:  Uninitialized string offset: 0 in /public_html/contactform.php on line 108

You are getting that notice because $contents is an empty string. What type of value should $contents be?

Link to comment
Share on other sites

You  are getting that notice because there is not a array index 3 in $alloweddomains. This is because your for loop is most likely iterating one too many times, due to you adding one to $domains in the condition. 

If  $alloweddomains is an array, then use a foreach loop instead

// loop over each allowed domain
foreach($alloweddomains as $domain) {
	if((stristr($_SERVER['HTTP_REFERER'], $domain))) {
		$allowed = 1;
	}
} 

You are getting that notice because $contents is an empty string. What type of value should $contents be?

 

Thanks for your detailed answer, I've replaced my code with the above and its no longer causing an error and the download still works so I'm assuming it works as intended :)

 

With regards to the unicode_replace_entities function. This is being passed a user inputted message from a message box on a contact form on the website. Its called as follows:

function previous_request_value($str) {
	if (isset($_REQUEST[$str]) )
		return $_REQUEST[$str];
	else
		return '';
	}

$message_body = trim(previous_request_value('message_body'));
$message_body=nl2br(htmlspecialchars(stripslashes($message_body), ENT_QUOTES, 'UTF-8'));
$oUnicodeReplace = new unicode_replace_entities();
$message_body = $oUnicodeReplace->UTF8entities($message_body);

Then the full class for unicode_replace_entities is:

class unicode_replace_entities {
	public function UTF8entities($content="") {
		$contents = $this->unicode_string_to_array($content);
		$swap = "";
		$iCount = count($contents);
		for ($o=0;$o<$iCount;$o++) {
		      $contents[$o] = $this->unicode_entity_replace($contents[$o]);
		      $swap .= $contents[$o];
		}
		return mb_convert_encoding($swap,"UTF-8"); //not really necessary, but why not.
	}

	public function unicode_string_to_array( $string ) { //adjwilli
		$strlen = mb_strlen($string);
		$array = "";
		while ($strlen) {
		      $array[] = mb_substr( $string, 0, 1, "UTF-8" );
		      $string = mb_substr( $string, 1, $strlen, "UTF-8" );
		      $strlen = mb_strlen( $string );
		}
		return $array;
	}
	
	public function unicode_entity_replace($c) { //m. perez
		$h = ord($c{0});   
		if ($h <= 0x7F) {
		        return $c;
		} else if ($h < 0xC2) {
		        return $c;
		}
           
		if ($h <= 0xDF) {
			$h = ($h & 0x1F) << 6 | (ord($c{1}) & 0x3F);
		        $h = "" . $h . ";";
			return $h;
		} else if ($h <= 0xEF) {
			$h = ($h & 0x0F) << 12 | (ord($c{1}) & 0x3F) << 6 | (ord($c{2}) & 0x3F);
			$h = "" . $h . ";";
			return $h;
		} else if ($h <= 0xF4) {
			$h = ($h & 0x0F) << 18 | (ord($c{1}) & 0x3F) << 12 | (ord($c{2}) & 0x3F) << 6 | (ord($c{3}) & 0x3F);
			$h = "" . $h . ";";
			return $h;
		}
        }
}

As you can tell some of this is not my code, and I can't confess to fully understanding all of it.

Edited by Bottyz
Link to comment
Share on other sites

thank you, this seemed to work. I did try a foreach loop before you sent this but ended up causing another error: PHP Warning:  Invalid argument supplied for foreach(). Your fix about makes perfect sense and so far has fixed my problem :)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

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.