I've managed to put together the following script:
<?php
/* make a URL small */
function make_bitly_url($url,$login,$appkey,$format = 'xml',$version = '2.0.1')
{
//create the URL
$bitly = 'http://api.bit.ly/shorten?version='.$version.'&longUrl='.urlencode($url).'&login='.$login.'&apiKey='.$appkey.'&format='.$format;
//get the url
//could also use cURL here
$response = file_get_contents($bitly);
//parse depending on desired format
if(strtolower($format) == 'json')
{
$json = #json_decode($response,true);
return $json['results'][$url]['shortUrl'];
}
else //xml
{
$xml = simplexml_load_string($response);
return 'http://bit.ly/'.$xml->results->nodeKeyVal->hash;
}
}
//function to get the url of the event!
function curPageURL() {
$pageURL = 'http';
if ($_SERVER["HTTPS"] == "on") {$pageURL .= "s";}
$pageURL .= "://";
if ($_SERVER["SERVER_PORT"] != "80") {
$pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
} else {
$pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
}
return $pageURL;
}
/* usage */
$short = make_bitly_url('http://site.com/viewEvent.php?id=2323232','bitlyuser','bitlyapikey','json');
echo 'The short URL is: '.$short . "<br>";
echo "PATH: ". curPageURL();
// returns: http://bit.ly/11Owun
?>
Now this code can produce the a short url of whatever is passed to it. I have a tweet button on my site that I got from twitter developer site. it works in that it posts the fully link of the page it is currently on...so not the shorten version. Now i want when that twitter button is pressed for it produce a short url so that I could share on my site's account. How is that done?
Thank you,
You should be able to just set the data-url option to the bitly url. e.g.
Tweet
Related
I am using this code to store the referrer URL in my form for tracking conversions, the problem I have is that the URL it stores is the URL which the form is on and not the URL where the visitor came from.
I am trying to get the URL of the website that the user came from, i.e if the user came from google I need the URL to be google.
Here's my code;
function hiddenreferer_shortcode($tag) {
if ( ! is_array( $tag ) )
return '';
$options = (array) $tag['options'];
foreach ( $options as $option ) {
if ( preg_match( '%^name:([-0-9a-zA-Z_]+)$%', $option, $matches ) ) {
$name_att = $matches[1];
}
}
$pageURL = 'http';
if ($_SERVER["HTTPS"] == "on") {$pageURL .= "s";}
$pageURL .= "://";
if ($_SERVER["SERVER_PORT"] != "80") {
$pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
} else {
$pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
}
$value_att = $pageURL;
$html = '<input type="hidden" name="' . $name_att . '" value="'.$value_att.'" />';
return $html;
}
// REFERER IN CONTACT FORM 7
wpcf7_add_shortcode('hiddenreferer', 'hiddenreferer_shortcode', true);
This may be a duplicate of:
Get original URL referer with PHP?
in which case, you're looking for:
$_SERVER["HTTP_REFERER"];
Edit: As Martin reminded me in the comments below, as with all data coming from outside your controlled environment - sanitise it!
Escape the character sequence if you handle it in PHP and probably a good idea to check it for SQL injection attempts if you store it in a database at all (and use PDO/prepared statements etc).
I would like to change the following code in the following way.
The php script gets the current url of the page of mine.
Now since I am using the descriptions of what i have on my another site i would like to put canonical tag on the page.
So I would like the script to get the url of my site and replace the www.mysite.com to www.domain2.com
How do i make it that the ,," will result the url i want? ( so i can make " rel="canonical" />
It can be beneficial for others aswell, who own more sites and have the same description and dont want to get penalized by Google Panda or have product descriptions from manufacturers.
<?php
function curPageURL() {
$pageURL = 'http';
if ($_SERVER["HTTPS"] == "on") {$pageURL .= "s";}
$pageURL .= "://";
if ($_SERVER["SERVER_PORT"] != "80") {
$pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
} else {
$pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
}
return $pageURL;
}
?>
I have finally found a solution to that
<?php
$Get_url = "http://" . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
echo str_replace("www.mydomain.com", "www.domain2.com", $Get_url);
?>
I am working on search function which I want make my search more easier and I had store href inside my db. Therefore, I need to get specific part of current page url eg : abc.php.
But now I only can get full url which is eg : http://abc_system/user/abc.php. Is it one of the solution is used substring?I am looking for some help. Hope you guys can help me out. Thanks in advanced.
This is my code which return url result:
function curPageURL() {
$pageURL = 'http';
if ($_SERVER["HTTPS"] == "on") {$pageURL .= "s";}
$pageURL .= "://";
if ($_SERVER["SERVER_PORT"] != "80") {
$pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
} else {
$pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
}
return $pageURL;
}
You need to use basename() function for get your filename from the URL string.
<?php
$url = "http://google.com/sdfsaf/abcd.php";
echo basename($url); // It will returns abcd.php
?>
Demo
Use **parse_url()** if you want to split url to its components,
For more info, Refer : http://www.php.net/manual/en/function.parse-url.php
I am trying to get the website url of the current page on which the person is. I would use it in the social share buttons that I am creating. The websites are all Wordpress websites. I got the following url for getting the current url,
<?php if (is_home()) { echo site_url(); } else { echo the_permalink(); } ?>
The script does not execute and when I click the button, the url does not generate but the php script is displayed in the browser as it is.
Please help out. Thanks
You would use
<?php echo $_SERVER['PHP_SELF']; ?>
This gives you the url of the current page. For example index.php or something similar
Using Javascript:
var currentURL = document.URL;
alert(currentURL);
Using PHP:
<?php
function curPageURL() {
$pageURL = 'http';
if ($_SERVER["HTTPS"] == "on") {$pageURL .= "s";}
$pageURL .= "://";
if ($_SERVER["SERVER_PORT"] != "80") {
$pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
} else {
$pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
}
return $pageURL;
}
?>
If you want file name
Use PHP Magic Constants such as __FILE__
i use a CMS (elgg : http://www.elgg.org ) working with view, equivalent to theme/template. To change the theme i have to put inside URL the term ?view=mytheme, like :
http://www.mydomain.com/index.php?view=mytheme
If i don't add ?view=mytheme, elgg choose the view by default
So, i need help, for specific users, i want to redirect us to a custom view, and can't see the default view.
I make this in my header :
<?php
if ((string) $_SESSION['user']->type === '2') {
// ???
} else {
echo 'do nothing';
}
I don't know how to take the current url, and simple add ?view=mytheme at the end ?
When a user have the type '2', and see the the url http://www.mydomain.com/index.php
it have to be forward to > http://www.mydomain.com/index.php?view=mytheme
Thanks.
This is kinda dirty, but you could:
// Stole this from here: http://webcheatsheet.com/php/get_current_page_url.php
function curPageURL() {
$pageURL = 'http';
if ($_SERVER["HTTPS"] == "on") {$pageURL .= "s";}
$pageURL .= "://";
if ($_SERVER["SERVER_PORT"] != "80") {
$pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
} else {
$pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
}
return $pageURL;
}
$useCustomView = ((string)$_SESSION['user']->type) === '2';
$usingCustomView = isset($_GET["view"]);
// Redirect to custom view if user type is 2 and view is not already set
if ($useCustomView && !$usingCustomView) {
$newurl = curPageURL() . '?view=mytheme';
header("Location: $newurl");
}
This is the most basic example I can think of to accomplish what you're asking. This won't quite do the job if you want the URL to support longer query strings so it needs some work.