Skip to content
Advertisement

Error when using “parse_str” … Undefined index: query

I am trying to get the current page URL, which may look something like this:

http://www.example.com/login.php/?redirect=myprofile.php

…and then get everything after the “redirect”. Here is my code so far:

JavaScript

However, I keep getting an error if the index query does not exists in the array:

Notice: Undefined index: query

I’ve tried isset:

JavaScript

which gives me a HTTP ERROR 500 error when loading the page.

and empty gives me the same error as the first.

JavaScript

I’m new to PHP, so any help would be much appreciated.

Advertisement

Answer

You don’t want to be checking the function call returns. You want to check that the query index exists. You can achieve that with:

JavaScript

parse_str can then be used when you have the query index.

https://3v4l.org/Vt3aa

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement