Help me please get the value of the address bar of browser without the parameters passed. Without the use of regular expressions and string functions. You can do this? (I use php on apache).
enter
http://dev.mazda-parts.ru/catalogue/?spattern=1
exit
http://dev.mazda-parts.ru/catalogue/
Take a look at the $_SERVER superglobal.
<?php
//example
echo $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URL'];
parse_url() can help you, or some of the php string functions, like strtok()
You say that you want the URL of the last page, which can be found in the $_SERVER['HTTP_REFERER'] variable.
Beware that this value is not reliable as it can be freely changed by the client.
If you want a more accurate way of finding the last page, you can use sessions. Here's an example:
session_start();
$last_page = $_SESSION['pageurl'];
$_SESSION['pageurl'] = $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URL'];
// $last_page now contains a more reliable value for the last url
Related
I have a page that does a re-direct as such, following the guidelines from this SO Post.
header("Location: http://www.fivefoo.com?uid=johnny");
die();
This small php file is located here in index.php of course.
http://www.fivefoo.com/johnny
What I did was just add on a query to the re-direct
?uid=johnny.
First and main question. Am I allowed to to do this?
Secondly, where do I retrieve the query values. My guess, would be the $_GET global.
Yes you are allowed to do this (why shouldn't you be?)
Yes, you can get the query values from the $_GET superglobal array. More specifically, $_GET['uid'] will contain the text 'johnny' (without the quotes of course).
Yes, you can do this.
Yes, the $_GET is used for this, so $_GET['uid'] in your example would return 'johnny'.
More info here
How do i get the URL of the current page minus all of the get arguments (?blah=2&blah4=90...)
I know i can get the full URL with $_SERVER['REQUEST_URI'] but i was wondering if there was something that more fit my needs.
Or should i just do strpos ? and substr to chop of the arguments? ( i imagine that a $_SERVER var would be more efficient - if one exists)
Thanks
$_SERVER['REQUEST_URL']
Sometimes answers are easy :) I found the solution here: http://php.net/manual/en/reserved.variables.server.php by CTRL+F'ing for "query".
EDIT
As matchu said in the comments, not all servers support REQUEST_URL. In that case I would use the much less elegant strtok($url, '?');.
$_SERVER['REQUEST_URL']
should do the trick...
It's listed here in the comments:
http://php.net/manual/en/reserved.variables.server.php
Don't know why it isn't in the doco?
You can use the parse_url function and then concat the parts you need(scheme, host, port, user, pass and path)
You can use this, is pretty simple:
$url = explode("?",$_SERVER[REQUEST_URI]);
$url = "http://$_SERVER[HTTP_HOST]/" . $url[0];
With exploding the URL you will have in $url[0] the "first part" and in $url[1] all the arguments.
I am very curious on how to do this. I want a PHP script to look at the string after the URL link and echo the value.
For example, if I entered:
"http://mywebsite.com/script.php?=43892"
the script will echo the value 43892. I have seen this in most websites, and I think it will be a very useful to have in my application.
Thanks,
Kevin
You mean, something like
http://mywebsite.com/script.php?MyVariable=43892
? Variables provided at the end of the URL like that are available in the $_GET array. So if you visited the above URL and there was a line on the page that said
echo $_GET['MyVariable'];
then 43892 would be echoed.
Do be aware that you shouldn't trust user input like this - treat any user input as potentially malicious, and sanitise it.
echo filter_var($_SERVER['QUERY_STRING'], FILTER_SANITIZE_NUMBER_INT);
The sanitation is because in your example the query string is =43892, not 43892. The filter used "remove[s] all characters except digits, plus and minus sign".
Don't you mean http://mywebsite.com/script.php?43892 ?
You can either use apache URL rewriting or try to extract all entries from $_GET and look a the one which looks like a number or simply doesn't have a value.
Try manually parsing the URL like this
$geturl = $_SERVER['REQUEST_URI'];
$spliturl = explode("?",$geturl);
$get
= explode("=",$spliturl[0]);
echo $get[1];
:)
Before I really answer your question, I just have to say that most sites - at least that I have seen - actually use ?43892, with the =. This is also much easier than using it with = in my opinion.
So, now to the actual answer. You can simply extra the query string using $_SERVER['QUERY_STRING'].
An example:
User requests index.php?12345:
<?php
echo $_SERVER['QUERY_STRING'];
?>
Output:
12345
Note that you can also use something like
<?php
if(substr($_SERVER['QUERY_STRING'], 0, 1) == '=') {
$query_string = substr($_SERVER['QUERY_STRING'], 1);
}else{
$query_string = $_SERVER['QUERY_STRING'];
}
echo $query_string;
to support ?=12345 as well as 12345, with the same result. Note also that ?=12345 would not be available as $_GET[''] either.
The way you usualy use query parameters is by assigning them like http://mywebsite.com/script.php?var1=123&var2=234
Then you will be able to access them by $_GET['var1'] and $_GET['var2'] in your PHP script
I'de recommand parse-url for this. The documentation contains all you (I think) need.
I use a query string, for example test.php?var=1.
How can I check if a user types anything after that, like another string...
I try to redirect to index.php if any other string (query string) follows my var query string.
Is it possible to check this?
For example:
test.php?var=12134 (This is a good link..)
test.php?a=23&var=123 (this is a bad link, redirect to index..)
test.php?var=123132&a=23 (this is a bad link, redirect to index..)
I'm not sure I fully understand what you want, but if you're not interested in the positioning of the parameters this should work:
if ( isset($_GET['var']) && count($_GET) > 1 ) {
//do something if var and another parameter is given
}
Look in $_SERVER['QUERY_STRING'].
Similar to Tom Haigh’s answer, you could also get the difference of the arguments you expect and those you actually get:
$argKeys = array_keys($_GET);
$additionalArgKeys = array_diff($argKeys, array('var'));
var_dump($additionalArgKeys);
test.php?a=23?var=123 (this is a bad link, redirect to index..)
In this case, you only have one variable sent, named "a" containing the value "a?var=123", therefore it shouldn't be a problem for you.
test.php?var=123132&a=23 (this is a bad link, redirect to index..)
In this case you have two variables sent, ("a" and "var").
In general you can check the $_GET array to see how many variables have been sent and act accordingly, by using count($_GET).
I think you are trying to get rid of unwanted parameters. This is usually done for security reasons.
There won't be a problem, however, if you preinitalize every variable you use and only use variables with $_GET['var'], $_POST['var'] or $_REQUEST['var'].
How can I use download.php?get=file.exe with without the get variable, like download.php?=file.exe, using the $_GET in PHP?
You can use $_GET[0] or $_REQUEST[0]
You could use $_SERVER['request_uri'] which would allow you to omit the ? completely, leaving you with URLs like example.com/download.php/file.exe
Then, with a bit of URL rewriting (or implementing a bootstrap controller) you could clean it up even more, resulting in example.com/download/file.exe
What you need i address rewritting this wikipedia article should give you enough information to stat with. Specifically, if you use apache, read about mod_rewrite.
You can use $_SERVER['QUERY_STRING'] to get everything after the ?.
Edit: Then you could use download.php?file.exe