This is something new to me, I have read and read on how to fix this. My code has came a long way since.
When I started this piece I was going to define every request like so:
if ($_REQUEST['do'] == 'remove') {
$theme = 'default theme applied';
setcookie("drcstyleaccent", "", time() - 3600, '/');
}
if ($_GET['accent'] == 'light') {
$theme = 'light';
setcookie('drcstyleaccent', 'light', time()+60*60*24*365, '/');
}
echo $theme . 'theme applied';
Every theme was going to be defined in a separate get request with a new cookie / varible in each.
Since beginning my undefined index search, I have upgraded my script to:
$accent = isset($_GET['accent']) ? $_GET['accent'] : '';
if ($_REQUEST['do'] == 'remove') {
setcookie("drcstyleaccent", "", time() - 3600, '/');
} else {
setcookie('drcstyleaccent', $accent, time()+60*60*24*365, '/');
}
echo $accent . ' theme applied';
From everything I have read this looks right to me, but I keep getting the undefined index error. From what I've read $accent = isset($_GET['accent']) ? $_GET['accent'] : '';
is defining the index...
I have tried also wrapping the do
request with an isset
but this should not be necessary because of the else right?
I know I can just disable the error with error_reporting(E_ALL & ~E_NOTICE);
but I done want a "cheat".
I am ready for the down votes, I KNOW this has been covered here. There are just too many topics on this to sift through them all, and all I have found have helped me to get to where I am now, and now I'm stuck.
Where am I going wrong with setting the index?
I have tried also wrapping the do request with an isset but this should not be necessary because of the else right?
No. By running $_REQUEST['do'] == 'remove'
, you are accessing $_REQUEST
at index 'do'
. If this index is not defined, the Undefined index error is produced. To counter this, prepend the equality test with an isset()
:
if (isset($_REQUEST['do']) && $_REQUEST['do'] == 'remove') {
// ...
}