• 8

A PHP Error was encountered

Severity: Notice

Message: Undefined index: userid

Filename: views/question.php

Line Number: 191


File: /home/prodcxja/public_html/questions/application/views/question.php
Line: 191
Function: _error_handler

File: /home/prodcxja/public_html/questions/application/controllers/Questions.php
Line: 433
Function: view

File: /home/prodcxja/public_html/questions/index.php
Line: 315
Function: require_once

name Punditsdkoslkdosdkoskdo

How to redirect to the same page in PHP

How can I redirect to the same page using PHP?

For example, locally my web address is:


How can I redirect within my website to another page, say:

header("Location: clients.php");

I know this might be wrong, but do I really need to put the whole thing? What if later it is not http://localhost/?

Is there a way to do something like this? Also, I have a lot of code and then at the end after it is done processing some code... I am attempting to redirect using that. Is that OK?

My preferred method for reloading the same page is $_SERVER['PHP_SELF']

header('Location: '.$_SERVER['PHP_SELF']);

Don't forget to die or exit after your header();

Edit: (Thanks @RafaelBarros )

If the query string is also necessary, use


Edit: (thanks @HugoDelsing)

When htaccess url manipulation is in play the value of $_SERVER['PHP_SELF'] may take you to the wrong place. In that case the correct url data will be in $_SERVER['REQUEST_URI'] for your redirect, which can look like Nabil's answer below:

header("Location: http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]");

You can also use $_SERVER[REQUEST_URI] to assign the correct value to $_SERVER['PHP_SELF'] if desired. This can help if you use a redirect function heavily and you don't want to change it. Just set the correct vale in your request handler like this:

$_SERVER['PHP_SELF'] = 'https://sample.com/controller/etc';
  • 83
Reply Report
      • 1
    • If the query string is also necessary, use header('location:'.$_SERVER['PHP_SELF'].'?'.$_SERVER['QUERY_STRING']); exit;.
      • 2
    • If you use .htaccess to redirect URL's to a single file, this will redirect to that single file and not the URL in the browser.
      • 2
    • I have problem with ampersand. Solution: header('Location:'. $_SERVER['PHP_SELF'].'?'. str_replace("&","&",$_SERVER['QUERY_STRING']));die;
    • @HugoDelsing If you're redirecting to a request handler, you can set $_SERVER['PHP_SELF'] = 'whatever you want'; I usually do this in the request handler itself, with pieces parsed out from some manual config values and $_SERVER['REQUEST_URI'] (split between each / via regex). This lets me use my redirect code from above without worry and also gives me granular control over my friendly url format. So when htaccess url manipulation is in play use $_SERVER['REQUEST_URI'] which you can use to assign the correct value to $_SERVER['PHP_SELF'] if desired.

There are a number of different $_SERVER (docs) properties that return information about the current page, but my preferred method is to use $_SERVER['HTTP_HOST']:

header("Location: " . "http://" . $_SERVER['HTTP_HOST'] . $location);

where $location is the path after the domain, starting with /.

  • 12
Reply Report
    • I i am not sure why it is not redirecting, maybe i did it wrong? $location= '/index.php'; header("Location: " . "http://" . $_SERVER['HTTP_HOST'] .$location);
      • 1
    • For debugging, simply output that string with echo instead of header. If it is correct, then something else is wrong (headers must be the first output on the page, for example.)

To really be universal, I'm using this:

$protocol = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' 
    || $_SERVER['SERVER_PORT'] == 443) ? 'https://' : 'http://';
header('Location: '.$protocol.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']);

I like $_SERVER['REQUEST_URI'] because it respects mod_rewrite and/or any GET variables.

https detection from https://stackoverflow.com/a/2886224/947370

  • 8
Reply Report

Warm tip !!!

This article is reproduced from Stack Exchange / Stack Overflow, please click

Trending Tags

Related Questions