Insert PHP variable in external link - php

The link works perfectly fine internally on the domain, just won't work for an external website. Didn't see anything in the documentation. I provided an example below. Would appreciate some insight.
<?php $user = basename($_SERVER['PHP_SELF']);?>
<a href="https://www.externaldomain.com/secure/cart/addItem.aspx?qty=1&itID=9135&PromoID=83&uid=<?php echo $user->username ?>">

Based on you comment, you must have a page url like page.php?ID=userid and you want something like this: {externalpage.com/page.php}?uid=userid
Try:
<a href="https://www.externaldomain.com/secure/cart/addItem.aspx?qty=1&itID=9135&PromoID=83&uid=<?php echo $_GET["ID"]; ?>">
Edit
Based on your last comment, try this:
<?php $user = basename($_SERVER['PHP_SELF']);?>
<a href="https://www.externaldomain.com/secure/cart/addItem.aspx?qty=1&itID=9135&PromoID=83&uid=<?php echo $user; ?>">

Related

Check if div ID exists in PHP

I have same php to generate HTML in 2 ocasions:
Generate a link with target="_new";
Generate a link without target property.
The only way that I have to differentiate both of them is to create the parent div as different ID (eg: <div id="new"> for the 1st, '' for the 2nd.
Is there any way to check if has some #new in html and them set target?
Here's the way that I've tried so far:
<?php $new = $html->find("#new"); ?>
<a href="<?php echo $item->getPrimaryLink()->getUrl(); ?>" <?php if (is_object($new)): ?> target="_new" <?php else : ?> <?php endif; ?> >
If you are following HTMLDOMPARSER then you can follow:
$html = file_get_html('http://www.google.com/');
$is_new_exist=false;
if($html->find('div#new'))
$is_new_exist=true;
And now you can use that flag for your checking
For further query please checkout HTMLDOMPARSER
I would presume you would be able to use something like
$new = $html->find("#new");
if ($new) {
echo something
} else {
echo something else
}
Based on the assumption you are using domdocument or a html parser.
You would not use target however but rather do something like <a href="#new" or if it were on another page <a href="somepage.php#new"

Simple location.href with php echo in in link

I have pretty straight forward question:
What would be the correct syntax for this be?
onclick="location.href = '/xxx/xxx?key='.'<? $value[0]->pass_key; ?>'"
At the moment, this redirect's to my page /xxx/xxx/?key, but leaves out my pass_key, which i need to generate data on the resulting page.
UPDATE
Was a mistake not to use
<?= ?>
which is the same as
<? echo ?>
I've updated it in my code still no key is passed along.
This statement resides in this tag:
<td>
SOLVED
Thx to chinnu and y'all!
And what's up with the downvote? :( just because it's a simple mistake/question. .
Prudes.
onclick="location.href = '/xxx/xxx?key=<?php echo $value[0]->pass_key; ?>'"

Check for/insert http in PHP/MySQL echo URL variable

I currently have a dynamic PHP page that displays values from a database to display a URL like this:
<a target="_blank" href="<?php echo($row['Virtual_Tour']); ?>"><?php echo($row['Virtual_Tour']); ?></a>
This works great EXCEPT when the data does not include a http://. I have not control over the data. How do I test to see if the echo starts with http:// and if not inserts that as well to guarantee the URL is properly formatted?
if(substr($row['Virtual_Tour'],0,7)=="http://")
{
//starts with http://, no formatting needed
}
else
{
$row['Virtual_Tour']="http://".$row['Virtual_Tour'];
}
You can just remove the exist http://, if it exists, and put a newone like below code
<a target="_blank" href="<?php echo "http://".str_replace("http://","",$row['Virtual_Tour']); ?>"><?php echo "http://".str_replace("http://","",( $row['Virtual_Tour']); ?></a>
you can do this also with https:// like
<a target="_blank" href="<?php echo "http://".str_replace("https://","", str_replace("http://","",$row['Virtual_Tour'])); ?>"><?php echo "http://". str_replace("https://","",str_replace("http://","",( $row['Virtual_Tour'])); ?></a>
If I get it correctly, if your url does not contain http:// you want to add it, since some answers provide solution to show it only like this:
http://something.com.
If I am right, you could use this:
$string = "something.com";
$string = substr($string,0,6) != 'http://' ? "http://".$string : $string;
echo $string;
working example

PHP echo link, adding more to it

I am setting up a wordpress website and want to alter a plugin to work with another so that when a news link is clicked it will open in a lightbox using an iframe. I believe the following instances to be where the link is made.
<a href="<?php echo $link; ?>
I am looking to add the following to the link, at the end.
?iframe=true&width=100%&height=100%" rel="prettyPhoto[iframes]"
I am unsure of how to combine the two. I am hoping by doing so that I correctly located and understood what needed to be changed. If you want to see the full PHP file I have added it to http://pastebin.com/sFqSb1Ha
While I wouldn't mind someone telling me the answer, I would be happy to just be pointed in the right direction for the knowledge I need to study to accomplish this.
Try this:
<a href="<?php echo $link . '?iframe=true&width=100%&height=100%"' . 'rel="prettyPhoto[iframes]"'; ?>
I can see that there are a lot of $link echos in the code you provided. To apply that attrubute for all, you may set $link before, like:
$link = $link . '?iframe=true&width=100%&height=100%" rel="prettyPhoto[iframes]';
(Note that " at the end is missing, because it's already provided in href="").
But if you want to use it for a single link, try
<a href="<?php echo $link; ?>?iframe=true&width=100%&height=100%" rel="prettyPhoto[iframes]" ...
There is just a little problem. If in $link there are parameters in URL, it will fail, because any other param must start with &, and we have ?iframe there. So it's good to check, whetever the ? sign occurs in $link or not.
if (strpos($link, '?') > 0)
$link = $link . '&iframe=true&width=100%&height=100%" rel="prettyPhoto[iframes]';
else
$link = $link . '?iframe=true&width=100%&height=100%" rel="prettyPhoto[iframes]';
Try this
<a href="<?php echo $link ."?iframe=true&width=100%&height=100%\" ". " rel=\"prettyPhoto[iframes]\" "; ?>
for echo use " you can use other varible in this

php multi lang site clean url

I'm developing a multilang site. The content generated on the varible that passes in the url. Exemple for about us page my url is: domain.com/file.php?id=1 I got one main file and in that file the query gets the id of the selected menu.
If I change the language my url turns to domain.com/file.php?id=1&lang=en. Every time I change the language my url adds one more lang like this: domain.com/file.php?id=1&lang=en&lang=fr&lang=de&lang=en.....
in other multilang project I used this: header("location: ".$_SERVER['SCRIPT_NAME']); But the it were less dynamic pages. like this: domains.com/aboutus.php. I mean: the number of pages were static. The user cannot add or remove pages.
This time because I pass the page id in the url I tried header("location: ".$_SERVER['SCRIPT_NAME'].'?'.$_SERVER['QUERY_STRING']); but it gives an redirect cycle error every time I try to change the lang.
UPDATE
Code to select the languages:
<?php $actual= $_SERVER["PHP_SELF"]."?".$_SERVER["QUERY_STRING"];?>
<div id="langContainer">
<span><a <?php if ($_SESSION['idLang']=='en') {echo"class='active'";}?> href="<?php echo $actual ?>&lang=en">EN</a></span>
<span><a <?php if ($_SESSION['idLang']=='fr') {echo"class='active'";}?> href="<?php echo $actual ?>&lang=fr">FR</a> </span>
<span><a <?php if ($_SESSION['idLang']=='es') {echo"class='active'";}?> href="<?php echo $actual ?>&lang=es">ES</a></span>
<span><a <?php if ($_SESSION['idLang']=='de') {echo"class='active'";}?> href="<?php echo $actual ?>&lang=de">DE</a></span>
</div>
in my session.php
if (!isset($_SESSION["idLang"]) )
$_SESSION["idLang"] = 'en';
if (#isset($_GET["lang"])){
$_SESSION["idLang"] = $_GET['lang'];
//header("location: ".$_SERVER['SCRIPT_NAME'].'?'.$_SERVER['QUERY_STRING']);
}
So my question is if there's anyway I can get my url cleaner, hidding the lang variables?
Thanks
Dynamically build query parameters, replacing exiting ones in the current URL:
<a href="...?<?php echo http_build_query(array('lang' => 'foo') + $_GET); ?>">

Categories