codeKracken codeKracken - 7 months ago 20
PHP Question

Is it possible to concatenate onto PHP $_SESSION variables?

I have an application that is using session variables. I am trying to pass the path of an image to another PHP page, but since the page is in another location my path breaks and the images won't display. Is there a way to concatenate onto the path in the

$_SESSION
variable?

page1.php


<?php
session_name('Private');
session_start();
$_SESSION['first_img'] = '<img src="img/first-img.png">';
$_SESSION['second_img'] = '<img src="img/second-img.png">';
?>


page2.php


<?php
session_name('Private');
session_start();
echo $_SESSION['first_img'];
echo $_SESSION['first_img'];
?>


since
page2.php
is in another folder, I would need to add
../
to the beginning of the
src
in the image path. I can't figure out how to add it to the
$_SESSION
variable.

Answer

one possible solution might consider storing the relative path in the session variable, and then wrapping the differences in your echo code.

page1.php

<?php 
session_name('Private');
session_start();
$_SESSION['first_img'] = 'img/first-img.png';
$_SESSION['second_img'] = 'img/second-img.png';
?>

page2.php

<?php 
session_name('Private');
session_start();
echo '<img src="../' . $_SESSION['first_img'] . '">';
echo '<img src="../' . $_SESSION['first_img'] . '">';
?>
Comments