error in php programming [duplicate] - php

This question already has answers here:
What causes: "Notice: Uninitialized string offset" to appear? [closed]
(5 answers)
Closed 9 years ago.
I have a text file write.txt:-write.txt
My name
jai hind
stack
I am getting the error in the following code at line 154 and 165 and the error is:-
PHP Notice: Uninitialized string offset: 11 in /home/sakshi/Desktop/syllabify/step2/Syll_script_1.php on line 154
PHP Notice: Undefined offset: -2 in /home/sakshi/Desktop/syllabify/step2/Syll_script_1.php on line 165
code:-
$file1 = fopen("write.txt", "r");
$q=1;
while ( $line = fgets($file1) )
{
$i=0;
$arr = explode( "\n", $line );
$q=$q+1;
if(strcmp($arr[0],"." ) == 0)
{
$i=1;
$q=$q-1;
echo "\nposition of dot in write.txt:";
echo "$i \t $q\n";
break;
}
}
fclose($file1);
if($i==0)
echo $i;
while ( $q!=1 )
{
$file_input=fopen("input.txt","r");
$line = fgets($file_input);
$p = strlen($line);
echo $p;
$a=2;
if ($p == $a)
{
$m = $line[0];
echo "$m\n";
$l=0; $h=0;
$filename=fopen("write.txt","r");
$content=file("write.txt");
for($i=0;$i<$u-1;$i++)
{
$arr=$content[$i];
$length=strlen($arr);
for($j=0;$j<=$length;$j++)
{
if(strcmp($arr[$j],$m ) == 0)// line 154
{
echo "\nYES PRESENT AT LINE NUMBER: ";
$h=$i+1;
echo "$h\n";
$l=$h+1;
}
}
break;
}
/********************************************************/
$arr3=$content[$l-2];// line 165
$ar3=explode(" ",$arr3);
$output_file=fopen("Result.txt","a");
fwrite($output_file,$ar3[0]);
fwrite($output_file," ");
fclose($output_file);
$output_file=fopen("Result.txt","a");
fwrite($output_file,$ar3[1]);
fwrite($output_file," ");
fclose($output_file);
$ar3=str_replace("\n","",$ar3);
$output_file=fopen("Result.txt","a");
fwrite($output_file,$ar3[2]);
fclose($output_file);
/************************************************************/
// put enter at the end of operation
$output_file=fopen("Result.txt","a");
fwrite($output_file,"\n");
fclose($output_file);
/**********************************************************/
}

The problem is in the following line:
for($j = 0; $j <= $length; $j++)
it should be
for($j = 0; $j < $length; $j++)
Without the '=' sign. Because with the equal sign the iteration will exceed the length of the string. Counting start at 0 until strlen()-1. Thus
$j < strlen();
Otherwise you should apply
$j <= strlen() - 1;

Check if $arr is an array before access to an element.

Related

Warning: Undefined array key in PHP 8.0

My webhotel have uppgrade to PHP version 8.0 The code is working but it's showing warningmessage on the homepage now.
I'ts something with my array.
Warning: Undefined array key "JS" in /customers/c/c/4/plf1x2.dk/httpd.www/test/svspeuro.php on line 1465
Warning: Undefined array key "Vinst" in /customers/c/c/4/plf1x2.dk/httpd.www/test/svspeuro.php on line 1465
Warning: Undefined array key "PWB" in /customers/c/c/4/plf1x2.dk/httpd.www/test/svspeuro.php on line 1468 Warning:
Undefined array key "Vinst" in /customers/c/c/4/plf1x2.dk/httpd.www/test/svspeuro.php on line 1468 Warning:
Undefined array key "oavgjord" in /customers/c/c/4/plf1x2.dk/httpd.www/test/svspeuro.php on line 1471 here
I Have tried with
enterif (isset($matchesLines[$Borta_Lag]['omgångar'])){
$matchesLines[$Borta_Lag]['omgångar']+=1;
} code here
but it doesnt work it's get a empty value.
Here is the code.
I hope someone can help me?
// Sorterar ut lagnamn och resultat
$file = file($filename);
$result = array_merge($file,$resultatarray)
$matchesLines = array();
foreach($result as $line) {
if(preg_match('/^([a-öA-Ö]+\D)-([a-öA-Ö]+\D) (\d+)-(\d+)/', $line, $data)){
$Hemma_Lag = $data[1];
$Borta_Lag = $data[2];
$Hemma_Resultat = $data[3];
$Borta_Resultat=$data[4];
$matchesLines[$Hemma_Lag]['Vinst'] += 0;
$matchesLines[$Borta_Lag]['Vinst'] += 0;
$matchesLines[$Hemma_Lag]['oavgjord'] += 0;
$matchesLines[$Borta_Lag]['oavgjord'] += 0;
$matchesLines[$Hemma_Lag]['Förlust'] += 0;
$matchesLines[$Borta_Lag]['Förlust'] += 0;
if ($Hemma_Resultat == $Borta_Resultat){
$matchesLines[$Hemma_Lag]['lag'] = $Hemma_Lag;
$matchesLines[$Hemma_Lag]['poang']+=1;
$matchesLines[$Hemma_Lag]['mål'] += $Hemma_Resultat;
$matchesLines[$Hemma_Lag]['måli'] += $Borta_Resultat;
$matchesLines[$Hemma_Lag]['oavgjord'] += 1;
$matchesLines[$Borta_Lag]['lag'] = $Borta_Lag;
$matchesLines[$Borta_Lag]['poang'] +=1;
$matchesLines[$Borta_Lag]['mål'] += $Borta_Resultat;
$matchesLines[$Borta_Lag]['måli'] += $Hemma_Resultat;
$matchesLines[$Borta_Lag]['oavgjord'] += 1;
}
if ($Hemma_Resultat > $Borta_Resultat){
$matchesLines[$Hemma_Lag]['lag'] = $Hemma_Lag;
$matchesLines[$Hemma_Lag]['poang']+=3;
$matchesLines[$Hemma_Lag]['mål'] += $Hemma_Resultat;
$matchesLines[$Hemma_Lag]['måli'] += $Borta_Resultat;
$matchesLines[$Hemma_Lag]['Vinst'] += 1;
$matchesLines[$Borta_Lag]['lag'] = $Borta_Lag;
$matchesLines[$Borta_Lag]['poang'] +=0;
$matchesLines[$Borta_Lag]['mål'] += $Borta_Resultat;
$matchesLines[$Borta_Lag]['måli'] += $Hemma_Resultat;
$matchesLines[$Borta_Lag]['Förlust'] += 1;
}
if ($Hemma_Resultat < $Borta_Resultat) {
$matchesLines[$Hemma_Lag]['lag'] = $Hemma_Lag;
$matchesLines[$Hemma_Lag]['poang']+=0;
$matchesLines[$Hemma_Lag]['Förlust'] += 1;
$matchesLines[$Hemma_Lag]['mål'] += $Hemma_Resultat;
$matchesLines[$Hemma_Lag]['måli'] += $Borta_Resultat;
$matchesLines[$Borta_Lag]['lag'] = $Borta_Lag;
$matchesLines[$Borta_Lag]['poang'] +=3;
$matchesLines[$Borta_Lag]['Vinst'] += 1;
$matchesLines[$Borta_Lag]['mål'] += $Borta_Resultat;
$matchesLines[$Borta_Lag]['måli'] += $Hemma_Resultat;
}
if (isset($matchesLines[$Borta_Lag])){
$matchesLines[$Borta_Lag]['omgångar']+=1;
$matchesLines[$Hemma_Lag]['omgångar']+=1;
}
}
}
Yes, PHP 8.0 has elevated this notice and a bunch of other notices to now be warnings :
A number of notices have been converted into warnings:
Attempting to read an undefined variable.
Attempting to read an undefined property.
Attempting to read an undefined array key.
...

slowAES decrypt to another key

There was a problem with the old slowAES library.
When trying to decrypt, in js it produces one, and in php it produces another.
There are a lot of errors in the console that I can’t figure out.
Tell me what's wrong? How to get the same keys?
<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
$a = "cd36b76f96b103402924bd5f76d3c204";
$b = "680eb6a492f48ea1b342aea7b79e18eb";
$c = "f746749b113236227058bd471f5c91dc";
function toHex($args){
if(func_num_args() != 1 || !is_array($args)){
$args = func_get_args();
}
$ret = '';
for($i = 0; $i < count($args) ;$i++)
$ret .= sprintf('%02x', $args[$i]);
return $ret;
}
function toNumbers($s){
$ret = array();
for($i=0; $i<strlen($s); $i+=2){
$ret[] = hexdec(substr($s, $i, 2));
}
return $ret;
}
function getRandom($min,$max){
if($min === null)
$min = 0;
if($max === null)
$max = 1;
return mt_rand($min, $max);
}
function generateSharedKey($len){
if($len === null)
$len = 16;
$key = array();
for($i = 0; $i < $len; $i++)
$key[] = getRandom(0,255);
return $key;
}
function generatePrivateKey($s,$size){
if(function_exists('mhash') && defined('MHASH_SHA256')){
return convertStringToByteArray(substr(mhash(MHASH_SHA256, $s), 0, $size));
}else{
throw new Exception('cryptoHelpers::generatePrivateKey currently requires mhash');
}
}
function convertStringToByteArray($s){
$byteArray = array();
for($i = 0; $i < strlen($s); $i++){
$byteArray[] = ord($s[$i]);
}
return $byteArray;
}
function convertByteArrayToString($byteArray){
$s = '';
for($i = 0; $i < count($byteArray); $i++){
$s .= chr($byteArray[$i]);
}
return $s;
}
include 'cryptovh/aes.php';
$aes = new AES();
$token = $aes->decrypt(toNumbers($c), 16, 2, toNumbers($a), 16, toNumbers($b));
echo toHex($token); // WHAT I HAVE
echo "<br>";
echo "016e9be78dd5130beb5febcd328ff588"; // WHAT I NEED
?>
cryptovh/aes.php
that is copy of: https://github.com/aleaxit/slowaes/blob/master/php/aes_fast.php
use this library:
https://github.com/aleaxit/slowaes
At the output, I get this token:
dd2f6d60b939b390dc19688babc3873d
And console errors:
Notice: Undefined offset: 16 in
/var/www/myuser/data/www/example.com/cryptovh/aes.php on line 386
Notice: Undefined index: in
/var/www/myuser/data/www/example.com/cryptovh/aes.php on line 386
Notice: Undefined offset: 20 in
/var/www/myuser/data/www/example.com/cryptovh/aes.php on line 386
Notice: Undefined index: in
/var/www/myuser/data/www/example.com/cryptovh/aes.php on line 386
Notice: Undefined offset: 24 in
/var/www/myuser/data/www/example.com/cryptovh/aes.php on line 386
Notice: Undefined index: in
/var/www/myuser/data/www/example.com/panel/cryptovh/aes.php on line
386
In slowaes/php/aes_fast.php, the inversion of the MixColumns operation in the mixColumns method is implemented incorrectly, the else-block must be:
...
} else {
for ($c = 0; $c < 4; $c++) {
$t[ $c] = self::$GEX[$state[$c]] ^ self::$GBX[$state[4+$c]] ^ self::$GDX[$state[8+$c]] ^ self::$G9X[$state[12+$c]];
$t[ 4+$c] = self::$G9X[$state[$c]] ^ self::$GEX[$state[4+$c]] ^ self::$GBX[$state[8+$c]] ^ self::$GDX[$state[12+$c]];
$t[ 8+$c] = self::$GDX[$state[$c]] ^ self::$G9X[$state[4+$c]] ^ self::$GEX[$state[8+$c]] ^ self::$GBX[$state[12+$c]];
$t[12+$c] = self::$GBX[$state[$c]] ^ self::$GDX[$state[4+$c]] ^ self::$G9X[$state[8+$c]] ^ self::$GEX[$state[12+$c]];
}
}
...
The inverse of the MixColumns operation is required for decryption.
There is also a typo in the method invMain, line 3, where i must be replaced by $i.
With these changes, the expected result is obtained, which can be verified e.g. here. The warnings are also no longer displayed.
I've filed an issue here. Note the ReadMe: The code is intended more for didactic purposes. In practice, openssl_encrypt / openssl_decrypt or similar should be used.

Php Undefined Offset Error While Printing Table

I'm tried to call the variable but I get this error Notice: Undefined offset: 6 in /Applications/XAMPP/xamppfiles/htdocs/databasetwo/untitled.php on line 113
This code print a table to Html I want to do it programmatically so the array can print the amount selected.
$id = "table" ;
// echo "<table id=$id><tr><th>Title</th><th>Price</th><th>Number</th></tr>";
echo "<table id=$id>";
foreach($rows as $row){
echo "<tr>";
for ($i = 0 ; $i <= $amount ; $i++) {
echo "<td>{$row[$i]}</td>";
/*echo "<td>{$row[1]}</td>";
echo "<td>{$row[2]}</td>";
echo "<td>{$row[3]}</td>";
echo "<td>{$row[4]}</td>";
echo "<td>{$row[5]}</td>";
echo "<td>{$row[6]}</td>"; */
}
echo "</tr>";
}
echo "</table>";
If I leave it $row[0], 1, 2 etc. Will Work. The error I'm getting is Notice: Undefined offset: 6 in /Applications/XAMPP/xamppfiles/htdocs/databasetwo/untitled.php on line 113
If you are starting from 0, make sure for ending criteria you add value - 1.
for ($i = 0 ; $i <= $amount - 1 ; $i++) {
Or remove = in loop like this,
for ($i = 0 ; $i < $amount ; $i++) {

Undefined variable for loop

I have a code in use and it generates a PHP error in the second "for" loop.
PHP Notice: Undefined variable: newmatches
if (empty($result['ERR'])) {
preg_match_all('(<h3><a[^<>]*href="([^<>]*)"[^<>]*>(.*)</a>\s*</h3>)siU', $result['EXE'], $matches);
for ($i = 0; $i < count($matches[1]); $i++) {
$matches[1][$i] = urldecode($matches[1][$i]);
preg_match_all('/\*\*(http:\/\/.*$)/siU', $matches[1][$i], $urls);
$newmatches[1][$i] = $urls[1][0];
}
for ($i = 0; $i < count($newmatches[1]); $i++) { //PHP Notice: Undefined variable: newmatches
if(strstr($newmatches[1][$i], $domain))
return $i+1;
}
} else {
return '0';
}
Thank you in advance!
I don't see anywhere where $newmatches would be set, aside from the first for loop, which won't run if count($matches[1]) is 0.
Not sure what all this hardcoding of your index to 1 is about, but a simple fix is to set $newmatches[1] = array() before the first loop.

php recursion - notice: undefined offset: 20 array

I'm working to parse the following string but at the same time trying to get rid of the current undefined offset on the index array. I would appreciate some help w/ the undefined offset issue.
error_reporting(E_ALL);
ini_set('display_errors', '1');
function my_recursion($String, &$Inc) {
$l = strlen($String);
$has_quotes = 0; $array = array();
$x= 0;
for ($Inc; $Inc < $l; $Inc++) {
$my_char = $String[$Inc];
if ($my_char == '(' && !$has_quotes) {
$Inc++;
$array[$x] = my_recursion($String, $Inc);
$x++;
} else if ($my_char == '"') {
$has_quotes = !$has_quotes;
if (!$has_quotes)
$x++;
} else if ($has_quotes) {
$array[$x] .= $my_char;
}
}
print_r($array);
}
$String = '(("HELLO"("BAR")("FOO")()""))';
$Inc = 0;
(my_recursion($String, $Inc));
To get rid of the errors, add this line to the start of your function:
$array = array();
and replace this line:
$array[$x] .= $my_char;
with this:
$array[$x] = isset($array[$x])? $array[$x].$my_char : $my_char;
For help with your recursion, you'll need to describe its desired behavior.

Categories