Jump to content

Recommended Posts

Hello,

 

Having the following php code:

 

if(!is_numeric($_GET['n'])) {
header("Location: /movies");
}

 

I am checking if the value I get is numeric, and if is not then redirect user to a specified page. But for some reason it does not work, the if statement does get executed because I tried with an echo and it works, but no redirection. Any ideas?

Link to comment
https://forums.phpfreaks.com/topic/130281-solved-php-header-regirect/
Share on other sites

is this file in the same directory as this executing file?

 

Yes:

if(!is_numeric($_GET['n'])) {
   header("Location: movies");
}

 

No:

if(!is_numeric($_GET['n'])) {
   header("Location: ../../movies");  // each "../" stands for: up one level
}

Ok, first off show us all the code , second off i think you have errors suppressed and have html before this header therefore it wont work, add

error_reporting(E_ALL);

to the top of the page and

ini_set("errors",1);

(i think)

 

Edit: Little guy , i think he said it isn't redirecting, not that it is redirecting to a 404

is this file in the same directory as this executing file?

 

Yes:

if(!is_numeric($_GET['n'])) {
   header("Location: movies");
}

 

No:

if(!is_numeric($_GET['n'])) {
   header("Location: ../../movies");  // each "../" stands for: up one level
}

 

Thanks a lot for your replies

 

First the reason is /movies is because it becomes like this http://beta.berwari.net/movies so when doing /movies that is assumed. and yes that is on the same folder.

 

<?php
if(!is_numeric($_GET['n'])) {
header("Location: http://beta.berwari.net");
}

 

That is the very top of the file.

here is the code minus header and footer.

 

<?php
if(!is_numeric($_GET['n'])) {
header("Location: /movies");
}else {		
@require_once(ROOT."/inc/config.php");
$id = $_GET['n'];
$result = mysql_query("SELECT * FROM movies WHERE id=$id");

if($row=mysql_fetch_array($result)){		
}else {
	header("Location: /");
}
}
?>
<h1>Manage Movies</h1>
<br />
<form name="editmovies" method="post" action="/manage_movies">
<table  cellspacing="0" cellpadding="0" class="frame">
  <tr>
    <td colspan="2" class="name" style="text-align:center"><input name="name" type="text" size="40" maxlength="100" value="<?=$row['moviename']?>" /></td>
  </tr>
  <tr>
    <td colspan="2" class="description" style="text-align:center"><textarea name="desc" id="desc" cols="45" rows="3"><?=$row['moviedesc']?></textarea></td>
    </tr>
  <tr>
    <td class="duration" style="text-align:left"><input type="text" name="duration" id="duration" /></td>
    <td class="date" style="text-align:right"><input type="text" name="date" id="date" value="<?=$row['postdate']?>" /></td>
    </tr>
  <tr>
    <td colspan="2" style="text-align:center"><input type="submit" name="btnEdit" id="btnEdit" value="Edit Movie" /></td>
    </tr>
</table>
</form>

Lolz, sorry to say that it is most likely the header that is causing the problem, move your code to the top of the page. There can't be any HTML before the code if there is it won't work and should produce an error (if you turned error reporting on)

here is a cheat :D put ob_start(); at the top of the code in the header.

 

I tried that, but I think how my site is setup, it goes in a never ending loop, so I can't use that. Index.php is the controller of the web site, so any request goes through index.php.

I'm trying to change the logic by avoiding the loop when using the ob_start but I don't know what to do anymore. I've tried almost everything. Here are my stuff...

 

Following is my htaccess code. The following code redirects everything to index.php, so if I do berwari.net/music it just like berwari.net/index.php?display=music. So therefore index.php is the controller.

RewriteEngine on
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^(.*)$ index.php?display=$1 [L,QSA]

 

The controller.

<?php session_start(); ob_start();
error_reporting(E_ALL);
ini_set("errors",1);

if (!isset($_GET['display'])){$_GET['display']="";}

switch(strtolower($_GET['display'])){
case "music":
	define("ADMIN",false);	
	define("TITLE","Berwari.net > Music");
	define("FILE","music.php");
	break;

case "contact":
	define("ADMIN",false);		
	define("TITLE","Berwari.net > Contact Us");
	define("FILE","contact.php");
	break;

case "about":
	define("ADMIN",false);		
	define("TITLE","Berwari.net > About Us");
	define("FILE","aboutus.php");
	break;

case "movies":
	define("ADMIN",false);		
	define("TITLE","Berwari.net > Movies");
	define("FILE","movies.php");
	break;

case "downloads":
	define("ADMIN",false);		
	define("TITLE","Berwari.net > Downloads");
	define("FILE","downloads.php");
	break;

case "links":
	define("ADMIN",false);				
	define("TITLE","Berwari.net > Useful Links");
	define("FILE","links.php");
	break;

	#---- Admin Stuff Begins ----
case "admin":
	define("TITLE","Berwari.net > Admin Section");
	define("FILE","admin/main.php");
	break;

case "manage_movies":
	define("ADMIN",true);
	define("TITLE","Berwari.net > Admin Section > Manage Movies");
	define("FILE","admin/manage_movies.php");
	break;

case "manage_downloads":
	define("ADMIN",true);	
	define("TITLE","Berwari.net > Admin Section > Manage Movies");
	define("FILE","admin/manage_downloads.php");
	break;

case "admin_logs":
	define("ADMIN",true);
	define("TITLE","Berwari.net > Admin Section > Logs");
	define("FILE","admin/logs.php");
	break;

	#---- Admin Stuff Ends ----

case "":
	define("ADMIN",false);		
	define("TITLE","Berwari.net > Home");
	define("FILE","main.php");
	break;
}

//if the user tries to get into admin area that 
//has admin as true then they'll be taken to /admin where they have to login
if (ADMIN==1){
if ($_SESSION['ADMINL']!=md5('Secret')){
	header("Location: /admin",);
}
}

@include_once(ROOT."/inc/head.php"); //header
@include_once(ROOT."/inc/sidebar.php");	//sibemenu
echo "<div id=\"column2\">";

@require_once(FILE);	//the body
?>
</div>
<?php
@include_once(ROOT."/inc/footer.php"); //Footer?>	

 

Thank you all!

From near the first line i noticed

if (!isset($_GET['display'])){$_GET['display']="";}

Wtf?

It doesn't make sense you are not doing anything in that line, except it should produce an error.

I changed your code a bit

<?php 
error_reporting(E_ALL);
ini_set("errors",1);
ob_start();
session_start(); 
//if (!isset($_GET['display'])){$_GET['display']="";} It does nothing
if(isset($_GET['display']))//check if the get var display is present, to stop errors
{
switch(strtolower($_GET['display'])){
   case "music":
      define("ADMIN",false);   
      define("TITLE","Berwari.net > Music");
      define("FILE","music.php");
      break;

   case "contact":
      define("ADMIN",false);      
      define("TITLE","Berwari.net > Contact Us");
      define("FILE","contact.php");
      break;

   case "about":
      define("ADMIN",false);      
      define("TITLE","Berwari.net > About Us");
      define("FILE","aboutus.php");
      break;

   case "movies":
      define("ADMIN",false);      
      define("TITLE","Berwari.net > Movies");
      define("FILE","movies.php");
      break;
   
   case "downloads":
      define("ADMIN",false);      
      define("TITLE","Berwari.net > Downloads");
      define("FILE","downloads.php");
      break;

   case "links":
      define("ADMIN",false);            
      define("TITLE","Berwari.net > Useful Links");
      define("FILE","links.php");
      break;

      #---- Admin Stuff Begins ----
   case "admin":
      define("TITLE","Berwari.net > Admin Section");
      define("FILE","admin/main.php");
      break;

   case "manage_movies":
      define("ADMIN",true);
      define("TITLE","Berwari.net > Admin Section > Manage Movies");
      define("FILE","admin/manage_movies.php");
      break;

   case "manage_downloads":
      define("ADMIN",true);   
      define("TITLE","Berwari.net > Admin Section > Manage Movies");
      define("FILE","admin/manage_downloads.php");
      break;

   case "admin_logs":
      define("ADMIN",true);
      define("TITLE","Berwari.net > Admin Section > Logs");
      define("FILE","admin/logs.php");
      break;

      #---- Admin Stuff Ends ----
   
   case "":
      define("ADMIN",false);      
      define("TITLE","Berwari.net > Home");
      define("FILE","main.php");
      break;
}
}
//if the user tries to get into admin area that 
//has admin as true then they'll be taken to /admin where they have to login
if (ADMIN==1)
{
   if ($_SESSION['ADMINL']!= md5('Secret'))
   {
      header("Location: /admin",);
   }
}

include_once(ROOT."/inc/head.php"); //header
include_once(ROOT."/inc/sidebar.php");   //sibemenu
echo "<div id=\"column2\">";
require_once(FILE);   //the body
echo "</div>";
include_once(ROOT."/inc/footer.php"); //Footer
?>   

While capturing the http header when the site goes in a loop: I get the following:

 


HTTP/1.1 302 Moved Temporarily
Date: Tue, 28 Oct 2008 00:16:57 GMT
Server: Apache/2.2.9 (Unix) mod_ssl/2.2.9 OpenSSL/0.9.8i DAV/2 mod_auth_passthrough/2.1 mod_bwlimited/1.4 FrontPage/5.0.2.2635
X-Powered-By: PHP/5.2.6
Expires: Thu, 19 Nov 1981 08:52:00 GMT
Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Pragma: no-cache
Location: /music
Keep-Alive: timeout=15, max=100
Connection: Keep-Alive
Transfer-Encoding: chunked
Content-Type: text/html

 

The expiry date has not been specified anywhere and also the response from server is 302.

 

Any ideas?

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.