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

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

Related

How can I add this api to wordpress to work automatic?

Guys I want to add shorted links from ouo .io to my wordpress site. I woudl like to use their API:
You can also use our simple API to shorten your link. The below link
will generate a new shorten link and print out in a blank page, it is
very easy to inject this API to your application.
http://ouo.io/api/WYTlzR4X?s=yourdestinationlink.com
I am adding links by additional filed (op1) in post:
<a href="<?php echo get_sub_field('op1'); ?>" rel='nofollow' target="_blank" class="prv">
I want to get that link shorted, because now I use it like:
<a href="http://ouo.io/s/WYTlzR4X?s=<?php echo get_sub_field('op1'); ?>" rel='nofollow' target="_blank" class="prawyklik">
what I get is:
http://ouo.io/s/WYTlzR4X?s=https://link-added-to-op1.com
and I want it to be:
http://ouo.io/39pkesT
I want to hide real links and show shorted. Please tell me how can I do it to work this way ?
Just to add to Chay22's answer, you should probably leverage transients so you're not constantly sending the same requests to the URL shortener API.
// Get our sub field and create an md5 hash
$sub = get_sub_field('op1');
$md5 = md5( $sub );
// Check if the url has been shortened before, if it hasn't...
if ( false === ( $url = get_transient( 'short-' . $md5 ))) {
// Grab the shortened url and save the transient for future use
$url = file_get_contents( 'http://ouo.io/s/WYTlzR4X?s=' . $sub )
set_transient( 'short-'. $md5, $url );
}
<a href="<?= $url ?>" rel="nofollow" target="_blank" class="prawyklik">
I'm sure that this can be done with file_get_contents
file_get_contents('http://ouo.io/s/WYTlzR4X?s=https://link-added-to-op1.com');
So it should be like this
<a href="<?php echo file_get_contents('http://ouo.io/s/WYTlzR4X?s='.get_sub_field('op1')); ?>" rel='nofollow' target="_blank" class="prawyklik">

Insert PHP variable in external link

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

How to display special html characters in title tag

I have a anchor tag that links to a php page, I would like to use special characters in the url parameter title
The string I would like to pass in the url:
Which is better approach to use href="#" or href="javascript:void(0)"
My anchor tag href will look like this:
mypage.php?title=Which is better approach to use href="#" or href="javascript:void(0)"
on my php page, when i echo $_GET["title"]; I only get part of the string:
Which is better approach to use href="
how to display exact title which i used in my anchor tag
You have to enconde the string before sending it to the title variable.
Example:
$title = 'Which is better approach to use href="#" or href="javascript:void(0)"'
echo '<a href="mypage.php?title='. urlencode($title) . '">';
// and to get the title
echo urldecode($_GET["title"]);
Use urlencode() to output your links and urldecode() to echo them in the other page:
First page:
<?php
$link = urlencode('Which is better approach to use href="#" or href="javascript:void(0)"');
echo 'a link';
?>
And on mypage.php you'll do:
<?php
echo urldecode( $_GET['title'] );
?>
The PHP $_GET['title'] is the variable "title" of the get array which are in fact the variables after the ? in an url.
test
this would show TheTitle
But you'll probably want to url escape the title.
Link

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); ?>">

How to set and get values from an url

I would like to set values on an url like this:
<a href='http://$data_url/viewyournote.php?chapter='$name_of_chapter'&note='$id_note'&user='$username'>
Then be able to grab then from the recieving page.
Right now all im getting is this when clicked:
http://localhost/readnotes/viewyournote.php?chapter=
I don't how you embed your link in your code, but if it is outside of <?php ?> tags, then you have to do:
<a href="http://<?php echo $data_url ?>/viewyournote.php?chapter=<?php echo $name_of_chapter ?>&note=<?php echo $id_note ?>&user=<?php echo $username?>" >
if it is inside these tags, you can also do:
echo "<a href='http://$data_url/viewyournote.php?chapter=$name_of_chapter&note=$id_note&user=$username?' >";
You can get these values on the recieveing page with $_GET['variable_name_here'], e.g. $_GET['chapter'].
Use Query string
$val = "yourvalue" ;
$url "http://localhost/readnotes/viewyournote.php?chapter=$val";
Now $val is passed to specified url .
There you could get it by using $_GET['chapter'] , It will give you "yourvalue"
<a href='http://$data_url/viewyournote.php?chapter=<?php echo $name_of_chapter; ?>&note=<?php echo $id_note; ?>&user=<?php echo $username; ?>>
Replace your line with
<?php
echo "<a href='http://$data_url/viewyournote.php?chapter='$name_of_chapter'&note='$id_note' user='$username'>";
?>
On the receiving end use
<?php
$name_of_chapter = $_GET['chapter'];
...
?>

Categories