codeKracken
codeKracken

Reputation: 190

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.

Upvotes: 0

Views: 2462

Answers (2)

Jeff Puckett
Jeff Puckett

Reputation: 40861

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'] . '">';
?>

Upvotes: 2

Petr Hejda
Petr Hejda

Reputation: 43491

Use absolute paths.

$_SESSION['first_img'] = '<img src="//'.$_SERVER["SERVER_NAME"].'/img/first-img.png">';
$_SESSION['second_img'] = '<img src="//'.$_SERVER["SERVER_NAME"].'/img/second-img.png">';

Upvotes: 1

Related Questions