0

I have a question..

function curPageURL() {
    $pageURL = 'http';
    if ($_SERVER["HTTPS"] == "on") {$pageURL .= "s";}
    $pageURL .= "://";
    if ($_SERVER["SERVER_PORT"] != "80") {
        $pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
    } else {
        $pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
    }
    return $pageURL.'?app_not_found';
}

Through the function I am fetching the current URL .... now I have two conditions

if($databaseAppVersion == $appVersionName)
    {
        //echo curPageURL();
        header('Location: '.curPageURL());
    }
    else 
    {
        $url ="www.google.com";
        //echo $url;
        header('Location: '.$url);
    }

when I am printing them the echo is happening but the problem is that the it does not redirection me to the intended page ... on the first condition I want to redirect to the current page having a message i.e. localhost/test/index.php?app_not_found but this is not happening

Dibyendu Konar
  • 175
  • 1
  • 1
  • 12

1 Answers1

0

if your page is like this:

<?php

function curPageURL() {
    $pageURL = 'http';
    if ($_SERVER["HTTPS"] == "on") {$pageURL .= "s";}
    $pageURL .= "://";
    if ($_SERVER["SERVER_PORT"] != "80") {
        $pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
    } else {
        $pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
    }
    return $pageURL.'?app_not_found';
}

if($databaseAppVersion == $appVersionName)
    {
        //echo curPageURL();
        header('Location: '.curPageURL());
    }
    else 
    {
        $url ="www.google.com";
        //echo $url;
        header('Location: '.$url);
    }

I wouldn't expect anything other than redirects to www.google.com, since you are not setting $databaseAppVersion and $appVersionName anywhere

if you have any echo or print functions before your header call, redirecting won't work, since the headers are already sent. See How to fix "Headers already sent" error in PHP.

on a different note; your curPageURL() will not necessarily return what you expect:

if you give it http://www.example.com it will return http://www.example.com https://www.example.com will return https://www.example.com:443 since you only check for port 80.

you can modify your code easily:

function curPageURL() {
    $pageURL = 'http';
    if ($_SERVER["HTTPS"] == "on") {$pageURL .= "s";}
    $pageURL .= "://";
    if ($_SERVER["SERVER_PORT"] != "80" || ($_SERVER["SERVER_PORT"] != 443 && $_SERVER["HTTPS"] == "on")) {
        $pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
    } else {
        $pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
    }
    return $pageURL.'?app_not_found';
}
Community
  • 1
  • 1
JoSSte
  • 2,953
  • 6
  • 34
  • 54