Is it possible to concatenate onto PHP $_SESSION variables? - php

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.

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

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">';

Related

PHP creating url with parameters

I am new to PHP and want to create a link with parameters.
So I used this :
<li>
<!-- $GLOBALS["ROOT_PATH"] is where my index.php file located -->
<a href="<?php echo $GLOBALS["ROOT_PATH"]."/Views/pages/profile.php"; ?>">
Profile
</a>
</li>
but when I click on the page it doesn't send me to the page or do anything. When I look at the URL it's something like file:///C:/bla/bla/bla/Views/pages/profile.php
Edit:
Basically, I want to use this in my header.php but my files are :
index.php
Views/pages/profile.php
and so on.
When I use the relative path for the header the path changes for these pages. How can I solve this?
So I solved it using some tricky way but it works :
function createLink($url,$text,$class){
echo "<li>";
// Gets the current php file name.
$basename = substr(strtolower(basename($_SERVER['PHP_SELF'])),0,strlen(basename($_SERVER['PHP_SELF']))-4);
if($basename !== "index"){
$url = "../../".$url;
}
echo '<a href="'. $url . '">';
echo '<span class="'.$class.'"></span>';
echo ' '.$text.'';
echo "</a>";
echo "</li>";
}
So it just add the relative path if the file name is not index.php.

Can a session variable be get and appended to a URL (PHP)

I had set and get the current page URL in a variable using session.
$currentURL = Mage::helper('core/url')->getCurrentUrl();
$currentPageSplit = explode('?___',$currentURL);
$currentPageURL = $currentPageSplit[0];
Mage::getSingleton('core/session')->setCurrentPage($currentPageURL);
session_start();
$curr = Mage::getSingleton('core/session')->getCurrentPage();
Now that I want this variable $curr to be appended to another URL used for switching from one page to another.
<a class="desktop" href="<?php echo $curr.'/?switch-view=desktop';?>">View desktop version</a>
I am unable to do this. Can someone tell if this syntax is right or what else could be done.
Thanks in adv
yes possible.
<?php
$sample=$_SESSION['alpha'];
echo 'click';
?> `
Try this.
To use the url in same page where you declare your session variable
<?php
$view = 'switch-view=desktop';
$_SESSION['view'] = $view;
$curr = $_SESSION['view'];
?>
<a class="desktop" href="<?php echo $curr;?>">View desktop version</a>
If you need to use the session in another page
<?php
session_start();
$curr = $_SESSION['view'];
?>
<a class="desktop" href="<?php echo $curr;?>">View desktop version</a>

php global vars with 'sourced' php files

I have my web site in html, with php chunks embedded. I define some variables in the main page as
<?php
$GLOBALS['myVar']= "something";
?>
later I have
<?php
echo '<p align="center"><img src="./tempImg.php">';
?>
the tempImg php file displays, using the phplot.php lib, a dynamic plot of some data read from the database and in the tempImg.php file I must use the myVar variable.
I tried using the GLOBALS[], the _SESSION[] but I am not able to share the variable in this way.
thanks for any help
$GLOBALS['myVar']= "something";
echo '<p align="center"><img src="./tempImg.php?myVar=' . $GLOBALS['myVar'] . '">';
Or just:
$myVar = "something";
echo '<p align="center"><img src="./tempImg.php?myVar=' . $myVar . '">';
Then access $_GET['myVar'] in tempImg.php.

Image tag src attribute not working in wordpress

I am creating a Google chart in wordpress, chart is rendered and stored into an image. However I am not able to call the image using <img src="" />. Following is the code:
$filepath = "/wp-content/uploads/graph.png";
file_put_contents($filepath, $response);
echo $filepath;
echo "<img src=\"/wp-content/uploads/graph.png\">";
I have also tried with http://*/graph.png which is not working. If I open the same in different browser, image is showing properly.
Try this
echo '<img src="' . get_bloginfo('template_directory') . '/images/logo.gif" />';
Go for 'template_directory' or 'stylesheet_directory'.
You may try the below code;
Much better if you combine your HTML code and PHP code. Much cleaner easy to read.
PHP code
<?php
Try changing your file path to
$filepath = "../../wp-content/uploads/graph.png";
file_put_contents($filepath, $response);
?>
HTML Code
<img src="<?php echo $filepath; ?>"/>

Test of file existence always fails

<?
$user_image = '../images/users/' . $userid . 'a.jpg';
if (file_exists($user_image))
{
echo '<img src="'.$user_image.'" alt="" />';
}
else
{
echo '<img src="../images/users/small.jpg" alt="" />';
}
?>
Hello all, this code is supposed to check for a file and if it doesnt work, display another.
For some reason it is ALWAYS displaying the placeholder and never finds the initial file even though it is there.
Is there something obviously not right here?
Thanks for reading!
the PHP is running in a different directory. try echo getcwd();
file_exists does not work with relative paths. Try something like this:
$user_image = $_SERVER{'DOCUMENT_ROOT'}.'/../images/users/' . $userid . 'a.jpg';
if (file_exists($user_image))
// blah blah
But, as Artefacto suggests, it's better to use the real path:
$user_image = '/path/to/your/files/images/users/' . $userid . 'a.jpg';
It's easier to maintain since you can use that code on different PHP scripts located on different directories without having to change anything.
If your relative path points outside of the htdocs subdirectories, then the image will not be sent by the webserver
Try using realpath and dirname instead.
<?
$user_image = '../images/users/' . $userid . 'a.jpg';
if (file_exists(realpath(dirname(__FILE__) . $user_image)))
{
echo '<img src="'.$user_image.'" alt="" />';
}
else
{
echo '<img src="../images/users/small.jpg" alt="" />';
}
?>
I mean I'm not always the smartest with php, but is your concat location correct? Because right now, won't it resolve to /images/users/userida.jpg ? is this really what you want?
I think you should check to see what realpath('../images/users/' . $userid . 'a.jpg') returns. I get the feeling it has something to do with the relative path

Categories