I have a sub-navigation menu on my page that opens up whenever ?n=review is appended to the URL.
I also have languages changing and session variables being set whenever ?language=xx is appended.
However, if I am on a page with the sub-nav open, and I wish to change language from there, the ?n=review is replaced with ?language=xx upon link click, and therfore the sub-nav closes. How would I check to see if a $_GET variable is already set every time the language is changed, and if so, append my additional one to the end?
Once the language has been changed, the new language is set to a session variable so I do not need to worry about keeping it's $_GET variable. I'm wondering if I would need to store the sub-nav variable in the same way...?
Sorry if this question is worded badly, I'm trying to get my head around the logic myself.
If you need to see any code or tidier wording, please let me know before down-voting.
if(empty($_GET['language'])) {
$fragment = '?n=review';
} else {
// if language is set, get the value
$lang = $_GET['language'];
$fragment = "?language=$lang&n=review";
}
// I just made up the /index.php part, replace that with the proper link.
$url = "/index.php$fragment";
Get variables can be chained by using the '&' Character. So you could build your link that way:
echo "./?n=review&language=xx"
If you want to know, if any $_GET parameters have been set, you can simply use PHP's empty() function like this:
if( empty( $_GET ) )
{
//$_GET is empty
}
else
{
//$_GET has something in it
}
Related
I'm trying to make an <a> link which triggers PHP code on the next page. I've tried using $_GET variables to do this but the thing is I also want to remove the variable afterwards, as I automatically link back to the redirected page with header(). There don't seem to be any feasible ways to do this without redirecting the user to one page alone, but the thing is they're expected to be redirected to the page they were on previously. Keeping $_GET variables then cause an endless loop of redirects.
In general, I wish to avoid using $_GET as it could be abused in the context I'm using it in. Any other workarounds would be greatly appreciated, though. Basically I'm just trying to use an <a> link to remove an entry from a MySQL database.
Here's the PHP that handles the variable.
if (isset($_GET['rm'])) # 'rm' contains the uuid of the entry to be deleted.
{
$uuid = $_GET['rm'];
unset($_GET['rm']); # Didn't expect this to work, of course it didn't remove the variable from the URL.
$query = "DELETE FROM posts WHERE uuid = '$uuid'";
$result = $mysqli->query($query);
header("Location: " . $_SERVER['REQUEST_URI']);
exit();
}
EDIT: I realize now that I have wildly complicated my explanation here. The main goal was to make the click of an <a> link trigger PHP code, with a variable specific to the link clicked. (Each link is a delete button on a post, and each post has a UUID)
If there is a way to alternatively trigger javascript code, that would be immensely helpful as well, since I'm looking to use such a method here too. I will likely be making a separate thread asking about this.
You can use $_SESSION to delete the variable after for example
if (isset($_SESSION['rm'])) # 'rm' contains the uuid of the entry to be deleted.
{
$uuid = $_SESSION['rm'];
unset($_SESSION['rm']); # Didn't expect this to work, of course it didn't remove the variable from the URL.
$query = "DELETE FROM posts WHERE uuid = '$uuid'";
$result = $mysqli->query($query);
header("Location: " . $_SERVER['REQUEST_URI']);
exit();
}
consider that you have register the value of the next shape.
$_SESSION['rm'] = "My value";
If your goal is to redirect to the current page but remove the query string, you can redirect to header("Location: ?"); which is essentially just that. (Technically you are redirecting to a new query string with no value which is different than no query string at all but php will just show an empty array for $_GET which is essentially the same)
I was going to mention additional options like variables from $_SERVER, but many of those have various security or other issues associated with them. I only mention this because I wouldn't suggest using any unless necessary. Also, it really doesn't get easier than the above.
How can I get the current inputed value of my parameter in url?
for example this is my url
"**www.example.com/register?redirect=**"
when I input a value in the redirect parameter for example is
https://www.google.com
how can I get that value of redirect?
The above comments are correct, but I wanted to take a second to explain why and provide some useful links.
$redirect = (isset($_GET['redirect'])) ? $_GET['redirect'] : false
Through this superglobal, you can access any parameter included in the URL. The values passed to you through this are urlencoded. YOu can read more about it here
What we did in the code above is this:
- Check if the redirect param hasbeen filled (you could aso just use empty() below )
- Assign the redirect value to our $redirect variable if it is set, otherwise assign it false.
Using that, you can redirect the user there if it's set:
if($redirect) {
header("Location: ".$redirect);
}
The downvote above isn't mine, but this isn't such a great question. I understand that you are new to the network, and everyone has to start somewhere. Please try to make sure you include code samples and do a bit of research yourself prior to your next question on SO.
At the moment a chuck of my site is running off using GETs to direct to a profile or a page. But what if you go to someone's profile page (so that's one GET) and you then click a sub tab on their profile which uses another GET, can you do this:
http://example.com/something.php?xyz=4example=6
I have seen facebook do this however I'm not sure where to look.
An alternate to this would be Javascript however I would rather do it with PHP if possible.
That should be
example.com/something.php?xyz=4&example=6
Note the ampersand '&' between the get vars.
To access the vars in php use
$xyz = $_GET['xyz'];
$example = $_GET['example'];
I am not sure ur exact question but in general if you want to pass values to a page through url u should do e.g.
http://example.com/page1.php?var1=val1&var2=val2....
Please note that each new variable after the first one has "&" before it. This tells the server that a new variable is expected, and the first variable has "?" before it, which tells the server to expect variables.
In the php page you can get the values of all the passed variables like
<?php
$_GET['var1']
$_GET['var2']
.
.
.
?>
and further user the values however you like. Note that you can not change a value in $_GET['var1']. If you want to change a value. First assign this value to a variable then further process. e.g
<?php
$var1 = $_GET['var1'];
$var1++;
?>
I have a website authored in PHP where any time a user receives an error I will redirect them to a another page (using header(Location:...)) and put the error ID in the URL so that I know which error to display.
E.g. If the user tries to access a product page but that item is no longer available I will redirect back to the category of items they were previously looking at and display an error based on the error ID I have specified in the URL.
www.example.com/view_category.php?product_category_id=4&error_id=5
There are two things I don't like about this approach:
It displays the error_id in the URL.
if the page is refreshed, the error will still display.
Is there a way to cleanly remove a specific $_GET variable from a URL while leaving the rest of the variables intact AFTER the page is loaded?
I'm thinking maybe it's using modRewrite or a redirect back to the page itself but removing the error_id from the URL or using a $_SESSION variable and avoiding putting the error_id in the URL. Your thoughts?
I really am learning a lot from this community and thought if I posed the question I might be able to learn something new or to get some varied ideas as I'm fairly new to scripting.
No, there's no way to do that explicitly - at least not without a page refresh but then you'd lose the data anyway.
You're better off using a temporary session variable.
if ( /* error condition */ )
{
$_SESSION['last_error_id'] = 5;
header( 'Location: http://www.example.com/view_category.php?product_category_id=4' );
}
Then, in view_category.php
if ( isset( $_SESSION['last_error_id'] ) )
{
$errorId = $_SESSION['last_error_id'];
unset( $_SESSION['last_error_id'] );
// show error #5
}
Yes, there is a way to remove especific $_GET from PHP...
varToRemove = "anyVariable";
foreach($_GET as $variable => $value){
if($variable != varToRemove){
$newurl .= $variable.'='.$value.'&';
}
}
$newurl = rtrim($newurl,'&');
Then, put the $newurl in the link.. like this:
pageurl?something=something&<? echo $newurl; ?>
I know it´s an old post, but, other programers may be search for it!
First, log the error in your database :)
After that, set a cookie or session variable and then redirect the user to safe page. When that page is loaded, have it check for the variable, display the error, and then delete variable from the cookie or session array.
One way is to compare the HTTP_REFERER with the SCRIPT_NAME. They'll be the same if the user has hit Refresh.
Quick Hack: You could have also imploded()'d on "&" in the the $_SERVER['QUERY_STRING'] variable to manipulate that string and then explode()'d it back.
Wouldn't this approach work?
<?php
$params = array_diff($_GET, array("variable_name" => $value));
$new_query_string = http_build_query($params);
?>
<script>window.history.pushState('verify_email', 'Verify Email', '?<?php echo $new_query_string; ?>');</script>
i had same problem
try : http://www.azazia.com/kb/entry/26/
if (!empty($_GET['passvar'])) {
unset($_GET['passvar']);
echo "<META HTTP-EQUIV=\"refresh\" CONTENT=\"0; URL=".$_SERVER['PHP_SELF']."\" >";
}
work perfectly for me.
How do I make it so that I can make a thing at the end of the address where the .php is and then tell it to do certain things. For example pull up a page like this:
sampardee.com/index.php?page=whatever
Help?
Anything else I could do with this?
This is generally achieved with the global php array $_GET. You can use it as an associative array to 'get' whatever variable you name in the url. For example your url above:
//this gives the $page variable the value 'whatever'
$page = $_GET['page'];
if($page == 'whatever'){
//do whatever
}
elseif($page == 'somethingelse'){
//do something else
}
Check out the php documentation for more information:
$_GET documentation
and there's a tutorial here:
Tutorial using QUERY_STRING and _GET
A small improvement over Brett's code:
if (array_key_exists('page', $_GET) === false)
{
$_GET['page'] = 'defaultPage';
}
$page = $_GET['page'];
// ... Brett Bender's code here
$_GET is usually used if you are sending the information to another page using the URL.
$_POST is usually used if you are sending the information from a form.
If you ever need to write your code so that it can accept information sent using both methods, you can use $_REQUEST. Make sure you check what information is being sent though, especially if you are using it with a database.
From your question it looks like you are using this to display different content on the page?
Perhaps you want to use something like a switch to allow only certain page names to be used?
i.e.
$pageName=$_REQUEST['page'];
switch($pageName){
case 'home':$include='home.php';break;
case 'about':$include='about.php';break;
case default:$include='error.php';break;
}
include($include);
This is a really simplified example, but unless the $page variable is either home or about, the website will display an error page.
Hope it helps!
I'm not quite sure what you're asking, but I think you're asking how to use GET requests.
Make GET requests against any PHP page as follows:
www.mysite.com/page.php?key1=value1&key2=value2
Now, from within PHP, you'll be able to see key1 -> value1, key2 -> value2.
Access the GET hash from within PHP as follows:
$myVal1 = $_GET['key1'] #resolves to "value1"
$myVal2 = $_GET['key2'] #resolves to "value2"
From here, play with your GET variables as you see fit.
The system of adding page parameters to a URL is know as HTTP GET (as distinct from HTTP POST, and some others less commonly used).
Take a look at this W3 schools page about GET in PHP and ahve a play about in getting parameters and using them in your PHP code.
Have fun!