I've noticed that instead of using:
http://yoursite.com/index.php?page=4
I could just use:
http://yoursite.com/index.php?4
How to do this in php and JavaScript?
In the example you have posted, now 4 is the key instead of value.
If you are passing ONLY one parameter via query string this is doable,otherwise you wont know in PHP what is what without a key for your parameter.
In the particular example you can get it like this in PHP
$id = null
if(!empty($_GET)){
$keys = array_keys($_GET);
// verify that the first key contains only digits, and use it as id
// ctype_digit does not work here because the key is now an int so use is_numeric
if(is_numeric($keys[0]))
$id = $keys[0];
}
I do not know what do you want to do in Javascript here.
Also if you are trying to generate SEO friendly URLs I would suggest that you look into URL Rewriting.
$page = $_SERVER['QUERY_STRING'];
Related
How can I read the values of get header variables which are from $_SERVER["HTTP_REFERER"] such as if I had index.php?col=example&order=example2 how could I read the values of col and order from the string which is received from $_SERVER["HTTP_REFERER"] and would this be safe?
I thought of using strpos() but that would mean I would have to make a function to find the position of col for example and then read the value starting from thr = and stop at the next & or null value if only one get header is set...
Based on a dupe, use parse_str, like this:
$str = 'index.php?col=example&order=example2';
$qs = parse_url($str, PHP_URL_QUERY);
if(!empty($qs)){
parse_str($qs, $output);
// TODO check for key existence
echo $output['col']; // example
echo $output['order']; // example2
}
The difference is that it won't work with index.php?, so we get just the query string part from the url.
I suggest you do some checking to make the script more reliable.
My page url just like that
http://wallpapers.wapego.net/main.php?ses=SuNpjmgtjmN7&f=3153038
now i want to take a part of current url that is 3153038 and store it as php integer variable
$_GET['ses'] will give you 'SuNpjmgtjmN7' and $_GET['f'] will give you '3153038'
Check $_GET
Use a GET super global or REQUEST super global variable.
ex -
$value = $_GET['f']; or $value = $_REQUEST['f'];
$_GET super global uses for get values from the URL. $_POST super global uses for put values to the server. I hope you understand it.
Provided your calling this URL in a 'get request' the value of 'f' will be in the $_GET array.
But the best way to store this as an integer value would be to parse it through intval()
$value = intval($_GET['f']);
Best way is to do it like this..
intval($_REQUEST['f']);
For example I have a URL like this one:
index.php?country=Canada
If it is just index.php that means that the default country is USA. People can switch between countries by checking or unchecking a checkbox.
But people can sort their results via GET variables:
State
Surname
Name
But if I use $_SERVER["REQUEST_URI"] then it will always just keep adding new values to my URL array (query string). It works if it is just index.php then I can make it like this:
State
Surname
Name
I know that after index.php it will always be a question mark ? first.
But what when visitors want to keep index.php?country=Canada and just switch between sort=state, sort=surname and sort=name. Then I need to know if a question mark is already in the URL, when to add & mark. I'm not sure how to solve this problem.
Change the way you echo your link:
PHP
<?php
$link = $_SERVER["PHP_SELF"];
if(isset($_GET['country']
$link.="&";
else
$link.="?";
?>
And echo link like this:
HTML
State
NOTE: I deleted the question mark before "sort".
You need logic to dynamically build a querystring, rather than statically adding ? and &.
Something like http_build_query would be the route I'd go, but the information you've provided is small compared with the possibilities you appear to want, so its hard to provide specific code.
Here is some more information about the http_build_query function in PHP. Its purpose is to build a querystring, here is an example of what you might do:
// capture the values into variables, however you want ($_GET is example)
$country = $_GET['country'];
$state = $_GET['state'];
$city = $_GET['city'];
$data = array();
// use logic to dynamically build the array, so long as the variables have values
!empty($country) ? array_push($data,'country'=>$country) : '';
!empty($state) ? array_push($data,'state'=>$state) : '';
!empty($city) ? array_push($data,'city'=>$city) : '';
// apply the array dynamically built
$querystring = http_build_query($data);
// concatenate to form the new URL
$url = 'http://www.example.com/?'.$querystring
If you declared country to be USA and city to be Seattle, but did not declare state, it would produce:
http://www.example.com/?country=USA&city=Seattle
Something along these lines should build the dynamic array you want with only the values you are looking for.
All of the variables in the query string are stored in the $_GET php superglobal array. In your example you can access the country with $_GET['country']. Do default to the USA you can use isset() to check if the variable exists.
$country = "USA";
if(isset($_GET['country'])) {
$country = $_GET['country'];
}
The solution here is to use either $_GET (already mentioned in another answer), or http://php.net/manual/en/function.parse-url.php, which makes the ordering of arguments completely irrelevant. Simply parse the url into an array of query arguments, and test for the ones you need. To turn it all back into a url, you would use http://php.net/manual/en/function.http-build-query.php
I need to extract a variable's value from a string, which happens to be a URL. The string/url is loaded as part of a separate php query, not the url in the browser.
The url's will look like:
index.php?option=com_content&view=article&catid=334:golfeq&id=2773:xcelsiors&Itemid=44
How can I always find & capture the value of the id which in this example is 2773?
I read several examples already, but what I've tried captures the id value of the current page which is being viewed in the browser, and not the URL string.
Thanks
You are looking for a combination or parse_url (which will isolate the query string for you) and parse_str (which will parse the variables and put them into an array).
For example:
$url = 'index.php?option=com_content&view=article&catid=334:golfeq&id=2773:xcelsiors&Itemid=44';
// This parses the url for the query string, and parses the vars from that
// into the array $vars (which is created on the spot).
parse_str(parse_url($url, PHP_URL_QUERY), $vars);
print_r($vars); // see what's in there
// Parse the value "2773:xcelsiors" to isolate the number
$id = reset(explode(':', $vars['id']));
// This will also work:
$id = intval($vars['id']);
echo "The id is $id\n";
See it in action.
You can use parse_str
You can use parse_url to parse URLs!
But you can use, to extract directly the numbers of ID variable:
$url = 'index.php?option=com_content&view=article&catid=334:golfeq&id=2773:xcelsiors&Itemid=44';
$id = preg_replace("/^.*[&?]id=([0-9]+).*$/",'$1',$url);
echo $id;
can anyone help me to play with GET urls for example I have a link like this:
?id=5&lang=1
So my question is how can I make this one:
?id=5,1
I don't want to show the &lang, only I want is that the &lang to replace with , "comma" can anyone help me?
You can use mod_rewrite to rewrite ?id=5,1 to ?id=5&lang=1 internally.
Otherwise, the value of id will be 5,1. Your application would then need to know that id contains more than the id. It could then parse out the language from the id. However, this will become confusing when you introduce more parameters.
Assuming you have already built the URL in the way you have specified, you can break the id field based on the comma and extract the real id and lang field
$urlPieces = explode(",", $_GET['id']);
$id = $urlPieces[0];
$lang = $urlPieces[1];
You are able to do this, but it's not very clean, in terms of the proper $_GET variable values. The solution automatically type casts the values to integers:
sscanf($_GET['id'], '%d,%d', $id, $lang);
// $id = int(5)
// $lang = int(1)
Two solutions:
Firstly, you could simply reformat the parameters when they arrive in your PHP program. With ?id=5,1, you'll get a PHP $_GET array with id '5,1'. This you can simply split using the explode() function to get the two values you want.
The second solution is to use the Apache mod_rewrite feature, to modify the URL arguments before they arrive at PHP. For this, you'll need to understand regular expressions (regex), as mod_rewrite uses this for it's work. You should google 'mod_rewrite' and 'regex' to find out more.
However mod_rewrite is typically used to get rid of GET arguments entirely. For example the URLs of the questions on this site do not have any get arguments, but the server translates the arguments between the slashes into GET arguments. This is considered better practice than simply than changing how the arguments look, as it is more user-friendly and SEO friendly.
Hope that helps.
$id = $id . ',' . $lang;
<a href="?<?php echo $id; ?>">