How to send destination parameter in URL when redirecting user if not logged in?

  authentication, localhost, parameters, php, redirect

I’m making a redirection on the login required pages.

If user is not logged in and he/she tried to open the login required page directly then he or she will be redirected to the login page with a destination parameter
Like this:-


And when it will redirect to login page then there will be check of destination at the top of the file

login_required_page.php :-

if(empty($user_id)) {
    redirect(BASE_URL . '/login? destination=' . $page_section);
    //page section is the page type ex. Login, signup, settings

Here is config file that contains the redirect function:-

function redirect($destination) {
    header("Location: " . $destination);

Here is the login page :-

if($_GET['destination'])) {
     $destination = $_GET['destination'];
     redirect(BASE_URL . '/' . $destination);
     //Only for checking I don't added redirection button onclick
     //Added redirection at top of the page so that I can check it's working fine or not

And the error is coming on the login page.that localhost redirected you so many times and page is not able to open!

Please help me to resolve this problem

Source: Ask PHP