1

I am trying to get dynamic $_SESSION[$id] on the second page shown below, but its not working (as per the printout):

First page url

https://example.com/test.php?id=1548393

First page code

<?php 
session_start();

$id = $_GET['id'];
$_SESSION[$id] = "mysecretstringline";

?>

Second page url

https://example.com/test2.php?id=1548393

Second page code

<?php 
session_start();
$id = $_GET['id'];

if(isset($_SESSION[$id])){
 echo "working";
}else{
 echo "not working";
}
?>
Mad Physicist
  • 107,652
  • 25
  • 181
  • 264
Hemang
  • 773
  • 2
  • 6
  • 12

2 Answers2

1

i found problem we can not use numeric index for $_SESSION

but we can use number in $_SESSION by convert number to roman numerals

first page url

https://example.com/test.php?id=1548393

first page code

<?php 
session_start();

$roman_id = romanic_number($_GET['id']);
$_SESSION[$roman_id] = "mysecretstringline";


        function romanic_number($integer, $upcase = true) 
        { 
            $table = array('M'=>1000, 'CM'=>900, 'D'=>500, 'CD'=>400, 'C'=>100, 'XC'=>90, 'L'=>50, 'XL'=>40, 'X'=>10, 'IX'=>9, 'V'=>5, 'IV'=>4, 'I'=>1); 
            $return = ''; 
            while($integer > 0) 
            { 
                foreach($table as $rom=>$arb) 
                { 
                    if($integer >= $arb) 
                    { 
                        $integer -= $arb; 
                        $return .= $rom; 
                        break; 
                    } 
                } 
            } 

            return $return; 
        } 

?>

second page url

https://example.com/test2.php?id=1548393

second page code

<?php 
session_start();

$roman_id = romanic_number($_GET['id']);

if(isset($_SESSION[$roman_id])){
 echo "working";
}else{
 echo "not working";
}


            function romanic_number($integer, $upcase = true) 
            { 
                $table = array('M'=>1000, 'CM'=>900, 'D'=>500, 'CD'=>400, 'C'=>100, 'XC'=>90, 'L'=>50, 'XL'=>40, 'X'=>10, 'IX'=>9, 'V'=>5, 'IV'=>4, 'I'=>1); 
                $return = ''; 
                while($integer > 0) 
                { 
                    foreach($table as $rom=>$arb) 
                    { 
                        if($integer >= $arb) 
                        { 
                            $integer -= $arb; 
                            $return .= $rom; 
                            break; 
                        } 
                    } 
                } 

                return $return; 
            } 

    ?>

output

working

thanks @gre_gor and @Katie

Hemang
  • 773
  • 2
  • 6
  • 12
0

Could be that in your normal code (this just looks like a quick mockup), you have a space after ?> somewhere. That could cause issues.

<?php
// start.php
session_start();
$id = $_GET['id'];
$_SESSION[$id] = "mysecretstringline";

and

<?php
// next.php
session_start();
$id = $_GET['id'];
if (isset($_SESSION[$id])) {
    echo "working";
} else {
    echo "not working";
}

works for me. Notice no ?> characters.

UPDATE:

The following might be of interest regarding session name constraints (can a php $_SESSION variable have numeric id thus : $_SESSION['1234’])

You have that issue in your example you could just add an id_ and then do the same check when validating/getting the session.

Community
  • 1
  • 1
Leon Vismer
  • 4,925
  • 1
  • 20
  • 22