Jump to content

divert page by .php?id=attribute


mastubbs

Recommended Posts

Hi there,

 

This is my first time using mySQL and i've managed to get a bit stuck.

 

What i want to do is redirect to a different page depending on the value of the attribute in the URL of a link. What i mean is:

 

If the url i type into the browser is http://www.mysite.com/product_info.php?products_id=1234 divert to http://www.mysite.com/some_page.php

 

if the url is http://www.mysite.com/product_info.php?products_id=2345 divert to http://www.mysite.com/some_other_page.php

 

I will be forwarding to a large number of pages (2000 or so) so i thought best to use a mySQL database rather than lots of if statements.

 

I can't seem to get it to work though, could anyone tell me what im doing wrong?

 

FIRST I make the table:

 

<?php 
// Make a MySQL Connection 
mysql_connect("localhost", "jasperss_redirect", "password") or die(mysql_error()); 
mysql_select_db("jasperss_redirect") or die(mysql_error()); 

// Create a MySQL table in the selected database 
mysql_query("CREATE TABLE redirect_table( 
id INT UNSIGNED NOT NULL AUTO_INCREMENT, 
PRIMARY KEY(id), 
page_list VARCHAR(50), 
file_name VARCHAR(150))") 
or die(mysql_error());    

echo "Table Created!"; 
?>

 

then,

 

 

<?php 
// Make a MySQL Connection 
mysql_connect("localhost", "jasperss_redirect", "password") or die(mysql_error()); 
mysql_select_db("jasperss_redirect") or die(mysql_error()); 

// Insert a row of information into the table "redirect_table" 
mysql_query("INSERT INTO redirect_table
(file_name, page_list) VALUES('some_page.php', '1234' ) ") 
or die(mysql_error());    

mysql_query("INSERT INTO redirect_table 
(file_name, page_list) VALUES('some_other_page.php', '2345' ) ") 
or die(mysql_error());    

echo "Data Inserted!"; 
?>

 

and on the redirect page itself (product_info.php)

 

<?php 
$pageid = (isset($_GET['products_id'])) ? (int) $_GET['products_id'] : null; 
if(!empty($pageid)) 
{ 
   if(mysql_connect('localhost', 'jasperss_redirect', 'password')) 
   { 
      if(mysql_select_db('jasperss_redirect')) 
      { 
         $result = mysql_query("SELECT file_name FROM redirect_table WHERE page_list = $pageid"); 
         if($result != false and mysql_num_rows($result)) 
         { 
            $page = mysql_result($result, 0); 
            header('Location: $page'); 
            exit; 
         } 
      } 
   } 
   $error = "Database error."; 
} 
else 
{ 
   $error = "No page specified."; 
} 
?> 
<html><head><title>Error</title></head><body> 
<p class="error"><?php echo $error; ?></p> 
</body></html>

 

 

When i type "http://www.mysite.com/product_info.php?products_id=1234" into the browser I get the error:

 

"The requested URL /$page was not found on this server.

 

Additionally, a 404 Not Found error was encountered while trying to use an ErrorDocument to handle the request."

 

It is redirecting me to $page by the looks of it, rather than to the actual value of $page from the database.

 

Can anyone help?

 

Thanks in advance,

 

Matt

Link to comment
Share on other sites

If you are doing massive grouping I would suggest looking into mod_rewrite. It is far far easier than php'ing everything through statements. But it can be somewhat intensive. So to clearify, if you wanted to group say, id's 1-500 to somepage1.html, then 501-1000 to somepage2.html then 1001-1500 to somepage3.html etc, it would probably be your best bet. Your pages will be read by whatever attribs you have, apache just redirects them to the pages you specify. In the above example you could do it all in 3 lines in your htaccess versus whatever you were looking for via php.

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.