Predefining $_GET parameter on open of index.php - php

Is there a way to set a $_GET parameter with certain value on open of "index.php"
For example: the url being = ".../index.php?somePara=value"
I tried using :
header("Location: index.php?page=1");
exit();
But I get an error of
"localhost redirected you too many times."
Edit:
Thanks, Qirel, that works, I need it because i have variables that are attached to $_GET parameter, so instead of setting them to NULL and then passing value to them after a if($_GET) check -> so that they wont get an error of undefined, I was wondering how pass value on open of the file.

The problem is either that you do not have a condition, or you have, but it is always true in this use-case.
Your question mentions the condition:
Is there a way to set a $_GET parameter with certain value on open of
"index.php"
but your code does not contain that:
header("Location: index.php?page=1");
exit();
So you can wrap this into a condition:
if (isset($_GET["certain"]) && ($_GET["certain"] === "value")) {
header("Location: index.php?page=1");
exit();
}
Note that the string parameter of header does not contain certain, since that would result in an infinite loop. You can also check in the if whether there is a page parameter if you intend to include certain there.
I have read the comment which suggested to set the corresponding key of $_GET to the expected value instead of a redirect, but that is not always an option, since you might want to beautify a URL or even to redirect to another site's page.

If $_GET parameters are used in my code, I always set a default value for these variables since you are never sure if it is set.
$name = isset($_GET['name']) ? $_GET['name'] : 'Guest';
// This is the shorter version for
if (isset($_GET['name'])) {
$name = $_GET['name'];
} else {
$name = 'Guest';
}
Your method of solving your problem will get you into an infinite loop. You could however do:
if (!isset($_GET['page'])) {
header("Location: index.php?page=1");
exit;
}

Related

Get the current inputed value parameter (PHP)

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.

How to check if any variable is being passed to GET in URL

I have a situation where someone is trying to sabotage my google adsense account by continuously sending personally identifiable information into the URL to my site. How can I block this or at least detect the random variables they are using?
For example, the variable name could be ANYTHING.
mysite.com/?asdfd=emailaddress#gmail.com
or
mysite.com/?gfrewgtr1=emailaddress#gmail.com
...?
The only thing I can think of doing is collecting known variables and then perform a header location redirect to the main site URL.
If: you want to have no GET parameters, check if $_GET is empty
if (!empty($_GET)) {
header('Location: ' . $_SERVER['SCRIPT_NAME']);
exit;
}
Or: check $_GET for non-allowed parameters:
$allowed_params = ["id", "some_param", "another one"];
foreach($_GET as $key => $val)
if (!in_array($key, $allowed_params)) {
// if something's wrong, get out!
echo('Location: '.$_SERVER['SCRIPT_NAME']);
exit;
}
// everything is ok here
Note: before any header()s you mustn't have any output. Otherwise you'll get an error. Better place the code in the very top of your script.
The $_GET reserved variable will contain any parameters passed in the URL.
var_dump($_GET);
Will output:
array(1) {
["asdfd"]=>
string(22) "emailaddress#gmail.com"
}
If there is anything in that array, you've essentially detected them. You could further use logic to weed out known query parameters you might use around your site and act accordingly if you find anything you deem actionable.

GET POST errors

All, I posted a code in a forum before and no one was able to answer this.
The "Sign Up" and "logout" are both buttons with value types on other pages that are linked to this page code called login.php
The problem is that I keep getting an undefined index. Is there a way to call it better?
I have..
if ($_POST['submit']=="Sign Up") {
and..
if($_GET["logout"]==1 AND $_SESSION['id']) { session_destroy();
header("Location:logout.php");
}
As both POST and GET variables must not be send at all, always use a scheme similar to this one:
$var = isset($_POST['fieldname']) ? $_POST['fieldname'] : null;
if ( !isset($var) )
{
// errorhandling
}
else
{
// proceed ...
The error you get indicates that at least one of your POST and/or GET variables is either not set or misspelled.
As Axel points out, check if $_POST has the value for "submit" using isset() function, if it is set, the you can access the value the way you are doing. If $_POST does not have the value for that, it means that it is not being sent.
So, in your HTML, check that the button is indeed called that way and that it is inside a tag with the ACTION attribute and METHOD set to POST.
Also be careful with comparing with 1, since in PHP is also means true.

Appending $_GET variable to URL already containing $_GET variables

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
}

How to add a GET variable to the current URL?

I have a question, i want to make some search page, and it needs a get variable to sort the results. So if someone enters the page without that GET variable, reload the page and make it appear, for example you enter www.myweb.com/search and automatically reloads and changes to www.myweb.com/search/?sort=ascending (because that variable is necessary) .
I hope you understand me, good bye
I think this will work for what you're looking to do:
if (empty($_GET['sort'])) {
header('Location: ' . $_SERVER['REQUEST_URI'] . '?sort=ascending');
exit();
}
From within the file executed when www.myweb.com/search is requested, you should have a default setting when $_GET['sort'] isn't available. For this Answer, I'll be using PHP for my examples since you didn't specify.
<?php
if (empty($_GET['sort'])) {
$sort = 'ascending';
}
// the rest of your code
Alternatively, you could force a redirect, but the previous example is more elegant.
<?php
if (empty($_GET['sort'])) {
header('Location: www.myweb.com/search/?sort=ascending');
exit;
}
Keep in mind, the second solution would throw away anything else, i.e., other $_GET's, like item=widget or color=blue
Note to others posting !isset as an answer. That will not work! Example:
www.myweb.com/search/?sort=&foo=bar
!isset($_GET['sort']) === false!
empty($_GET['sort']) is the proper route to take in this circumstance.
It is better to define the variable by yourself rather then redirecting. Just check with isset if the variable is defined or not. It it has not been defined you can set it yourself as below.
if(!isset($_GET['sort']))
{
$_GET['sort']='ascending";
}

Categories