Jump to content

Redirect issue


dhimok

Recommended Posts

Hi!

 

I have this script where I want to redirect if no entries found before the rest of the script is executed.

 

$sql = dbQuery("SELECT * FROM tbl WHERE id = '".$_GET["id"]."'");
if(mysql_num_rows($sql) == 0) {
  // do redirect to another page
}

// the code above is included on top of page and the one below in body

// here s the other code that gets the record ID from querystring
$sql = dbQuery("SELECT * FROM tbl WHERE id = '".$_GET["id"]."'");
// the rest

 

As known I get the:

Warning: Cannot modify header information - headers already sent by

 

and no redirection

 

Thanks

Link to comment
Share on other sites

What I am trying to do is to redirect visitors if they try to misuse querystrings

 

www.mysite.com/?id=400

 

If id matches the database record then show the info about that id

If id doesnt exist in the database then redirect

 

Thats what I will try to accomplish.

Link to comment
Share on other sites

Here s the code

 


<?php
// == required files ==
require_once("config/config.inc.php");
require_once("config/opendb.php");

// redirect
$sqlRedir = dbQuery("SELECT * FROM table WHERE id = '" . $_GET["id"] . "'") or die(mysql_error());
if(mysql_num_rows($sqlRedir) == 0) {
	header("location: ". ROOT_DIR. "/");
}

function getItem($id) {

$retval = '';
if (isset($_GET["item"])) {
    	$result = dbQuery("SELECT * FROM table WHERE id = '".$id."'");
    	$p = dbFetchObject($result, MYSQL_BOTH);
      		
		ob_start();
			include('includes/item.php');
			$retval .= ob_get_contents(); 
		ob_end_clean(); 
    	} 

return $retval;
}

?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Website name</title>
</head>

<body>

<?=getItem($_GET["id"])?>


</body>
</html>

 

I get Problem loading page and this . Using firefox

 

Firefox has detected that the server is redirecting the request for this address in a way that will never complete.

Link to comment
Share on other sites

>

Here s the code

 


<?php
// == required files ==
require_once("config/config.inc.php");
require_once("config/opendb.php");

// redirect
$sqlRedir = dbQuery("SELECT * FROM table WHERE id = '" . $_GET["id"] . "'") or die(mysql_error());
if(mysql_num_rows($sqlRedir) == 0) {
	header("location: ". ROOT_DIR. "/");
}

function getItem($id) {

$retval = '';
if (isset($_GET["item"])) {
    	$result = dbQuery("SELECT * FROM table WHERE id = '".$id."'");
    	$p = dbFetchObject($result, MYSQL_BOTH);
      		
		ob_start();
			include('includes/item.php');
			$retval .= ob_get_contents(); 
		ob_end_clean(); 
    	} 

return $retval;
}

?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Website name</title>
</head>

<body>

<?=getItem($_GET["id"])?>


</body>
</html>

 

I get Problem loading page and this . Using firefox

 

Firefox has detected that the server is redirecting the request for this address in a way that will never complete.

 

Link to comment
Share on other sites

that means u are redirecting to a page that redirects back the main which redirects back the redirect page which redirects back to the main.... and so on.

 

I find it helps to draw it out.

 

Index.php -redirects- Redirect.php

Redirect.php -redirects- Index.php    <----There is your problem. You are redirecting between the same 2 pages forever in a circle.

 

Link to comment
Share on other sites

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.