3

I have the following error in PHP error.log

[Tue Dec 19 12:08:22.887574 2017] [:error] [pid 32196] [client xx.xx.xx.x:20560] PHP Notice: Undefined offset: 8 in /var/www/html/page.php on line 55, referer: view.php?1x=8

and the php code that i think causes this is:

$i = isset($_SESSION['i']) ? $_SESSION['i'] : 0;
// echo $_SESSION['websites'][$i];
$website = explode(";", $_SESSION['websites'][$i]);
// echo $website[0];
$i++;
$_SESSION['i'] = $i;

I dont really know what $i = isset($_SESSION['i']) ? $_SESSION['i'] : 0; does

thank you

session_start();
$i = isset($_SESSION['i']) ? $_SESSION['i'] : 0;
if ($_SESSION['i'] < $sesioni1x) {  
    // echo $_SESSION['websites'][$i];
    $website = explode(";", $_SESSION['websites'][$i]);
    // echo $website[0];
    $i++;
    $_SESSION['i'] = $i;
        header("Location: $website[0]"); //redirect
        die();
        // echo $website[0];
        // echo $sesioni1x;
        // echo $website[0]." Frame-URL<br>";
        // $_SESSION['actual_website'] = $website[0];
}           
if ($_SESSION['i'] == $sesioni1x) {
    $handle = fopen($list1x, "a"); //open file to append content to csv file
    fputcsv($handle, $_SESSION['addwebsite'], ";"); //insert line in opened file
    fclose($handle); //close file
    header("Location: index.php"); //redirect
    die(); 
    // echo "session = var";
    }

this is the full code where i get this warning, i must say that script is doing his job, but i want to get rid of the error

the $_SESSION['i'] in the source is 0

Dharman
  • 30,962
  • 25
  • 85
  • 135
C4rm3K
  • 31
  • 3

3 Answers3

5

this is ternary called ternary operator. Basically it is a simple if/else.

$i = (isset($_SESSION['i']) ? $_SESSION['i'] : 0);

To help you understand it's this code in 1 line

if(isset($_SESSION['i'])){
    $i=$_SESSION['i'];
}
else{
    $i=0;
}
pr1nc3
  • 8,108
  • 3
  • 23
  • 36
2
$i = isset($_SESSION['i']) ? $_SESSION['i'] : 0;

The above code is just an if/else nothing else this will check for the $_SESSION['i'] is it is set then assign it's value to $i otherwise it assigns 0 to $i variable.

Now looking to your problem, it looks like your $_SESSION['i'] is already set and its value is 8 so $i = 8,

Now, $website = explode(";", $_SESSION['websites'][$i]); this like is checking for your $_SESSION['website'] array and trying to find 8th element from the array which has not been set so it gives an error its undefined.

Prateik Darji
  • 2,297
  • 1
  • 13
  • 29
1

(e) ? r1 : r2 is a conditional statement. If the expression e is true the value is r1, if e is false the value is r2. So

$i = isset($_SESSION['i']) ? $_SESSION['i'] : 0;

means "i is $_SESSION['i'], if $_SESSION['i'] is set, otherwise it is 0".

Palle Due
  • 5,929
  • 4
  • 17
  • 32