codeKracken codeKracken - 7 months ago 38
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 wont display. Is there a way to concatenate onto the path in the session variable?

the code looks like this
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 but I cant figure out how to add it to the session variable. Any help is appreciated. Thanks.

Answer

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

the code looks like this 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'] . '">';
?>