href but no url afterwards? - php

I have a php script that outputs a link element within a list element depending on certain conditions being met, the code is as follows:
global $wpdb;
function currentURL() {
$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;}
$getUrl = $wpdb->get_results("SELECT option_name,option_value FROM wp_options WHERE option_name = 'siteurl' ");
$url = $geturl->option_value;
$getData = $wpdb->get_results("SELECT post_title,guid FROM wp_posts WHERE post_type = 'page' ");
echo "<ul id=\"mainNavInnerContainer\">";
if(currentURL() == $url){
echo "<li>home</li>";}
else{ echo "<li>home</li>"; }
The problem I'm having is that when I view the element in source code view in my browser i get the following:
<a href>Home</a>
I'm really confused as to why this is, and was hoping that anyone else would have any idea why.
EDIT
I was able to use a foreach loop to put the value in my $url variable but is this good practice? Because I know that the query in $getUrl will always return one row, but still this method seems pretty error prone. Here is the revised $url variable code:
foreach($getUrl as $urlResult){
$url = $urlResult->option_value;}

add to top of code error_reporting(E_ALL);
add check:
$getUrl = $wpdb->get_results("SELECT option_name,option_value FROM wp_options WHERE option_name = 'siteurl' ");
$url = $geturl->option_value;
var_dump($url);

When you view the source of a page, some browsers (Firefox is one, I believe) show you there source they're rendering, not the source you sent them. If the attribute has no value specified, the browser may chose to display it like this, with the attribute present but without a value.

Related

Referrer URL in PHP

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).

Update a paremeter in a url instead of adding a new one

having a bit of trouble. Basically I have created some pagination. The problem is each time I click on a page number url it just adds the parameter to the url even if it already exists.
so for instance I land on the page. My url is now example.com/page?pagenum=1, I click the second page so my url is now example.com/page?pagenum=1&pagenum=2. Now it all works fine but as you can imagine is going to get a bit messy so would rather it update the parameter that's already in the URL. I'm currently using the following to get the 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;
}
?>
and then the Link is something like:
<a href='<?php echo curPageURL(); ?>&pagenum=<?php echo "1"; ?>'> 1 </a>
Update
I have other paremeters in the URL I need to keep, I only need to update 'pagenum'
The problem exists because REQUEST_URI contains both the path and query string, and you're appending a new query string to that every page turn. To extract the path, you could use this code, taken from this answer:
$path = strtok($_SERVER["REQUEST_URI"], '?');
You can then copy existing query string fields, but remove pagenum:
$fields = $_GET;
unset($fields['pagenum']); // remove any existing pagenum value
$path .= '?' . http_build_query($fields); // re-append the query string
You could then use more or less your existing link code:
<a href='<?php echo $path; ?>&pagenum=<?php echo "1"; ?>'> 1 </a>
You can use http_build_query like so:
$all_params = $_GET;
$all_params["page"] = "2";
$link = "page.php?" . http_build_query($all_params); // "page.php?page=2&foo=bar"

Get current page title without loop

I have a small script which should get the meta title of the current page the script is added into. The problem is, that its working fine on several test pages, but not into my CMS. It loops until death there and I cant reach any page on my server until I restart apache completely and by taking the script off.
May someone take a look at it? This would be really awesome since I used google for hours and sure, I found X threads and pages, but never a solution for this special loop-effect.
<?php
function curPageURL() {
$pageURL = 'http';
$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;
}
?>
<?php
if (!isset($_GET['ignore']))
{
$url = curPageURL();
$data = implode("", file("$url?ignore=this"));
preg_match ("/<title>([^`]*?)<\/title>/", $data, $match);
$urltitle = $match[1];
}
?>
<?echo $urltitle;?>
The $_SERVER["REQUEST_URI"] can also include GET params like this:
mysite.com?param1=1&param2=2
Then you try to append a string ?ignore=this so you get
mysite.com?param1=1&param2=2?ignore=this
which is translated by PHP into variables like
param1 = '1'
param2 = '2?ignore=this'
You must check for ? symbol in the $url variable
I'm using this function to get the current page url :
function currentURL() {
$protocol = stripos($_SERVER['SERVER_PROTOCOL'], 'https') === FALSE ? 'http' : 'https';
$host = $_SERVER['SERVER_NAME'];
$port = $_SERVER["SERVER_PORT"];
$query = $_SERVER['REQUEST_URI'];
return $protocol.'://'.$host.($port != 80 ? ':'.$port : '').$query;
}
But your problem comes from here :
if (!isset($_GET['ignore']))
{
$url = curPageURL();
$data = implode("", file("$url?ignore=this"));
/* ... */
}
This will work with "test pages", but you CMS propably use url-rewriting, which can cause the lost of your $_GET['ignore'] variable : if you've already other GET variable for example.
You should have a look into your .htaccess files, or read your CMS documentation to know what can change you url.
Anyway, it seems you're building some unstable code, and this only to get the page title. I'm pretty sure you've got another way to get it easily with your CMS.

PHP posting to twitter in bitly url format

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

How to add a custom value to current URL

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.

Categories