Page 1 of 1

Variables with a variable?

Posted: Mon May 19, 2008 2:06 pm
by spacebiscuit
Hi,

I am using variable to store html links:

$data="/home/$_SESSION[link]";

I am using the variable as follows:

Code: Select all

<?
echo"< a href=$data>Link here</a>";
?>
Is it possible to output the variable and the variable within the variable. At present with the abovce code it simple outputs:

"home/$_SESSION[link]"

Thanks in advance,

Rob.

Re: Variables with a variable?

Posted: Mon May 19, 2008 2:15 pm
by emmbec
Concatenate the variable $_SESSION[link] to your original variable:

Code: Select all

$data="/home/".$_SESSION[link];

Re: Variables with a variable?

Posted: Mon May 19, 2008 3:59 pm
by spacebiscuit
That is no tpossible i nmy case as the variable data is stored in a mysql database.

The links are pulled from the database row by row and all vary. In this particular case the row/link is:

'/home/$_SESSION[link]'

Any ideas?

Thanks,

Rob.

Re: Variables with a variable?

Posted: Mon May 19, 2008 4:30 pm
by emmbec
Here is what I did and it works, just modify it to your needs.

Code: Select all

$_SESSION[link]="hello_world.php";
$data = "link/test/$_SESSION[link]";
echo"<a href=\"".$data."\">Link here</a>";

Re: Variables with a variable?

Posted: Mon May 19, 2008 4:38 pm
by emmbec
robburne wrote:Hi,

I am using variable to store html links:

$data="/home/$_SESSION[link]";

I am using the variable as follows:

Code: Select all

<?
echo"[b]< a[/b] href=$data>Link here</a>";
?>
Is it possible to output the variable and the variable within the variable. At present with the abovce code it simple outputs:

"home/$_SESSION[link]"

Thanks in advance,

Rob.
You have an extra space there, remove it too:

Code: Select all

echo"[b]<a[/b] href=$data>Link here</a>";
That should work.

Re: Variables with a variable?

Posted: Mon May 19, 2008 6:13 pm
by spacebiscuit
It is not quite that simple guys.

the variable $_SESSION[link] is written on the fly and not known in advance of the code execution. So in effect when link is read from the database the $_SESSION[link] part is simply a reference to some dynamic data.

Here is my exact code.

From the database the link row is:

$link_data="/home/$_SESSION[link]";

I am actually adding the link data to a html map - over a grpahic:

<area shape="rect" coords="<? print_r($cordinates)?>" href="<? echo"$linkdata ?> ">

It is almost as if within the link_data variable I need to exit the echo and print the $_SESSION[link] variable.

I hope I am making sense here.

Thanks,

Rob.

Re: Variables with a variable?

Posted: Mon May 19, 2008 6:33 pm
by VladSun
I don't think it's a good software solution - does every record in the DB contain the $_SESSION['link'] string? If, so - remove it and concatenate at execution time. If not - using eval() might be a solution, but I would advice you to reconsider your solution.

Re: Variables with a variable?

Posted: Mon May 19, 2008 7:00 pm
by spacebiscuit
Thanks for your feedback guys.

Vladsun - the eval method works perfectly, why is it not a good solution?

In answer to your question, every record does not contain the session link.

Thanks,

Rob.

Re: Variables with a variable?

Posted: Mon May 19, 2008 7:05 pm
by VladSun
robburne wrote:Vladsun - the eval method works perfectly, why is it not a good solution?
Because eval() is security vulnerable by nature :)
I don't know where do you get contents for your DB rows and $_SESSION from and whether they could be injected with a malicious PHP code.