0

I'm trying to make a special parameter that when it's accessed it opens another php file on my webserver , Here's what I've been trying to do

<?php
$id = ($_GET['id']) ? $_GET['id'] : $_POST['id'];
switch($id)
{
case 'account':
require 'pages/cp.php';
break;
}
?>

So when the user authenticates to , let's say : http://www.test.com/?id=account , It should automatically authorize the user to the file cp.php .

Problem is that it doesn't redirect , It just remains on the index.php file .

Note : The above PHP code exists in the index.php !

Thanks!

Gumbo
  • 643,351
  • 109
  • 780
  • 844
  • 1
    does cp.php contain a redirect (`header()`) call? – andrew Sep 06 '14 at 11:55
  • Have you tried to echo `$id` variable before switch-case? What does it show? – Giorgio Sep 06 '14 at 11:55
  • @andrew , No the cp.php doesn't have a header call as I have used it before without header() calls and it worked . – Ahmed Magdy Sep 06 '14 at 11:56
  • `require` doesn't send the browser to another page, it just imports the content from it and outputs it to the current page (index). you need `header` for that http://stackoverflow.com/questions/768431/how-to-make-a-redirect-in-php – andrew Sep 06 '14 at 11:58
  • I don't use require to redirect , I use this echo''; – Ahmed Magdy Sep 06 '14 at 12:00
  • @AhmedMagdy well then I suggest you edit the question and explain the situation more clearly – andrew Sep 06 '14 at 12:00
  • Found a way around that , Thanks for your patience and help ! – Ahmed Magdy Sep 06 '14 at 12:02

1 Answers1

1

require() just like require_once() or include() will actually just include the file into the current script... it basically puts the source from 'pages/cp.php' right there - where you include it.

If you want to redirect the user to a new URL/Link/File you should use header:

header("Location: http://www.example.com/"); 

Just make sure you have no output (not even some HTML in the php file, or a whitespace outside of (pretty common mistake)) at all before you set the header or it will fail.

If you need output before you send header, make sure to use ob_start(); and ob_flush() - google, stackoverflow or the php-manual will help you there.

user3567992
  • 592
  • 5
  • 15