Solved

Php snippet pulling in full url.  I just want relative url

Posted on 2013-12-27
7
238 Views
Last Modified: 2014-08-03
Hello PHP experts,

I am trying to troubleshoot an image link issue.   I have sent an email to the developer and he will eventually get back to me.  But maybe not quickly.

In the meantime I think I have found the root of the issue.

There is some js in the application that is referencing the various images.  But instead of just putting in the relative url it is pulling in the full absolute url.  I want the relative url, not the absolute url.

I know next to nothing about php, but I found this snippet in some javascript in his application.

image_markup: '<img id="fullResImage" src="{path}" />',

Open in new window

I am thinking that "{path}" is built-in php placeholder code for full url.  Perhaps there is some other placeholder other than  "{path}" that will give me just the relative path.

However, as I write this question, I just found another snippet.  This is a php snippet.  And I am thinking that maybe it is controlling the display of the url.  Here it is:
if(strstr($element_setting['image'],"http://")){
	$image_url=$element_setting['image'];
}
else{
	$image_url="../".$element_setting['image'];
}
?>
<h5><?php echo $element_attribute['name'];?></h5>
<div style="max-width:300px;"><img src="<?php echo $image_url;?>"/></div>

Open in new window

As I look at the above snippet I am thinking that it is more likely the absolute url culprit.

Nevertheless, what the current code is giving me (and I don't want) is <img alt="" src="http://www.domainname.com/images/EmployeePhotos/martha.jpg" /></p>

What I want is a relative path, such as <img alt="" src="/images/EmployeePhotos/martha.jpg" /></p>

I thought while I wait for the developer to reply I would ask your thoughts here.  Of course I have a backup of the files.  I thought maybe you can reply quicker than the developer.  After all, this is a holiday weekend, isn't it?

I realize this question may be an exercise in futility without the full code from the application, but I am hoping that the snippets of code may offer a clue to a solution from the EE php experts. :)

Thanks!

Rowby
0
Comment
Question by:Rowby Goren
  • 3
  • 3
7 Comments
 

Assisted Solution

by:PaNes
PaNes earned 250 total points
Comment Utility
Let's start with the potentially easiest solution:  try changing {path} to {$path}

Now, assuming that didn't work, the beginning of your code is likely causing the problem.
 
if(strstr($element_setting['image'],"http://")){
	$image_url=$element_setting['image'];
}

Open in new window


Your code assigns the file location prefix to your image location.

Essentially, this says that if the image URL includes http://, then just the link as-is.
If it doesn't contain http://, then it goes to your ELSE condition.

else{
	$image_url="../".$element_setting['image'];
}
   

Open in new window


This says that if the file *wasn't* found to have an http:// prefix already, that it should instead be assigned a "../" prefix, where ../ indicates a relative location.  

Good news!  The way strstr works is that it returns the value that *follows* the matching condition, so what you want is doable, I think.

If every image is hosted at www.bobsfinechina.com, then simply change the code from

if(strstr($element_setting['image'],"http://")){
	$image_url=$element_setting['image'];
}

Open in new window


To

if(strstr($element_setting['image'],"http://www.bobsfinechina.com")){
	$image_url="../".strstr($element_setting['image'],"http://www.bobsfinechina.com");
}

Open in new window


note the
 
.

Open in new window

Between "../" and $element_setting['image'];

That basically adds the two together.

So in summary, you are adding ../ to the results of the strstr query function, which should give you the relative path you desire.
0
 
LVL 108

Accepted Solution

by:
Ray Paseur earned 250 total points
Comment Utility
Comments annotating the code snippet above:

// EXAMINE THE VARIABLE IN THE $element_setting ARRAY AT THE POSITION NAMED 'image'
// IF THIS VARIABLE CONTAINS THE SUBSTRING "http://"
if(strstr($element_setting['image'],"http://")){

    // COPY THE VARIABLE INTO THE $image_url VARIABLE
    $image_url=$element_setting['image'];
}
// IF THIS VARIABLE DOES NOT CONTAIN THE SUBSTRING "http://"
else{
    // PREPEND "../" TO THE VARIABLE AND THEN COPY THE VARIABLE INTO THE $image_url VARIABLE
    $image_url="../".$element_setting['image'];
}
// THE $image_url VARIABLE WILL BE INJECTED INTO THE HTML DOCUMENT BELOW
?>
<h5><?php echo $element_attribute['name'];?></h5>
<div style="max-width:300px;"><img src="<?php echo $image_url;?>"/></div>

Open in new window

What I think we're seeing here is a construct that will allow any explicit URL (one containing HTTP://) to go through unalloyed.  Other URLs will be made relative to the directory one level "up" from this directory.

You may have to experiment with it a bit, but something along these lines might get what you want.

// REMOVE THE EXPLICIT DOMAIN NAME FROM THE VARIABLE IN THE $element_setting ARRAY AT THE POSITION NAMED 'image'
$image_url = str_replace('http://domain.com/', '../', $element_setting['image']);

// THE $image_url VARIABLE WILL BE INJECTED INTO THE HTML DOCUMENT BELOW
?>
<h5><?php echo $element_attribute['name'];?></h5>
<div style="max-width:300px;"><img src="<?php echo $image_url;?>"/></div>

Open in new window

0
 
LVL 9

Author Comment

by:Rowby Goren
Comment Utility
Hi Ray,

Below is how I implemented your code.  Am not sure if I did it correctly:

defined( '_JEXEC' ) or die;
$element_key=$item_params['element_key'];
$element_setting=$item_params['element_setting'];
$element_attribute=$item_params['element_attribute'];
$element_shortcode=$item_params['element_shortcode'];
	   
if(strstr($element_setting['image'],"http://")){
// REMOVE THE EXPLICIT DOMAIN NAME FROM THE VARIABLE IN THE $element_setting ARRAY AT THE POSITION NAMED 'image'
$image_url = str_replace('http://www.blahblah.edu/', '../', $element_setting['image']);
}
// THE $image_url VARIABLE WILL BE INJECTED INTO THE HTML DOCUMENT BELOW
?>
<h5><?php echo $element_attribute['name'];?></h5>
<div style="max-width:300px;"><img src="<?php echo $image_url;?>"/></div>

Open in new window


I think I did something wrong because below is the result:

<hr id="system-readmore" />[yee_row ex_class=""][yee_column width="1/1" ex_class=""][yee_single_image title="" image="http://www.blahblah.edu/images/HASOM_fp_photos/martha-resized.jpg" etc etc....  

Open in new window


Basically it did not remove the http://www.blahblah.com.

If it looks right I will paste it back in.  But please double check my implimentaiton when you have a chance.

Thanks

Rowby
0
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 108

Expert Comment

by:Ray Paseur
Comment Utility
You might want to give it a try this way.  You don't need the if() statement, but you might want this: http://www.php.net/manual/en/function.str-ireplace.php

If this doesn't remove the domain name, there are a few other possibilities.  One is that this is not the code that generates the <img> tag.  The other is that the domain name is not spelled correctly, or contains HTTPS instead of HTTP, etc.

defined( '_JEXEC' ) or die;
$element_key=$item_params['element_key'];
$element_setting=$item_params['element_setting'];
$element_attribute=$item_params['element_attribute'];
$element_shortcode=$item_params['element_shortcode'];
	   
// REMOVE THE EXPLICIT DOMAIN NAME FROM THE VARIABLE IN THE $element_setting ARRAY AT THE POSITION NAMED 'image'
// THE $image_url VARIABLE WILL BE INJECTED INTO THE HTML DOCUMENT BELOW
$image_url = str_ireplace('http://www.blahblah.edu/', '../', $element_setting['image']);
?>
<h5><?php echo $element_attribute['name'];?></h5>
<div style="max-width:300px;"><img src="<?php echo $image_url;?>"/></div>

Open in new window

0
 
LVL 108

Expert Comment

by:Ray Paseur
Comment Utility
One other thing to check... This piece of code:
<h5><?php echo $element_attribute['name'];?></h5>
<div style="max-width:300px;"><img src="<?php echo $image_url;?>"/></div>

Open in new window

Cannot possibly create this:
<hr id="system-readmore" />[yee_row ex_class=""][yee_column width="1/1" ex_class=""][yee_single_image title="" image="http://www.blahblah.edu/images/HASOM_fp_photos/martha-resized.jpg" etc etc....

Open in new window

0
 
LVL 9

Author Comment

by:Rowby Goren
Comment Utility
Hello PaNes and Ray,

Well I guess the code is pulling in the url some other way, because it is not replacing the url string.    

Let's hold off for now and hopefully the developer will get back to me this weekend and I will report his "solution".

Stay tuned!

And thanks!

Rowby
0
 
LVL 9

Author Closing Comment

by:Rowby Goren
Comment Utility
Thanks Ray and Panes,

Here are you points.  Sorry for the delay!

Rowby
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now