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

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"

Related

How to get specific part of 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

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

Getting url of the current page

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__

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.

href but no url afterwards?

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.

Categories