Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
How to Redirect based on page?
Old 05-19-2010, 03:21 PM How to Redirect based on page?
Extreme Talker

Posts: 193
Name: Adam Moseley
Trades: 0
I need to redirect pages on my site to other pages based on the page name on my site.

My site is a Url shortener, when you type in a website it assigns it a key using mysql db starting with 1 then 2 etc. How do I make the site redirect a user coming to my site with the link, http://mydomain.com/1 to the full url assigned to the key '1'?

Thanks

Aadam
__________________

Please login or register to view this content. Registration is FREE
aadam is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 05-19-2010, 03:23 PM Re: How to Redirect based on page?
Average Talker

Posts: 15
Trades: 0
Using header location:

http://php.net/manual/en/function.header.php

Code:
<?php
/* This will give an error. Note the output
 * above, which is before the header() call */
header('Location: http://www.example.com/');
?>

__________________

Please login or register to view this content. Registration is FREE
AskTechGuy is offline
Reply With Quote
View Public Profile
 
Old 05-19-2010, 03:26 PM Re: How to Redirect based on page?
Extreme Talker

Posts: 193
Name: Adam Moseley
Trades: 0
But how do I relate that to the url in the database that matches the page?

Thanks
aadam is offline
Reply With Quote
View Public Profile
 
Old 05-19-2010, 03:35 PM Redirection regarding database
Extreme Talker

Posts: 193
Name: Adam Moseley
Trades: 0
I need to redirect pages on my site to other pages based on the page name on my site.

My site is a Url shortener, when you type in a website it assigns it a key using mysql db starting with 1 then 2 etc. How do I make the site redirect a user coming to my site with the link, http://mydomain.com/1 to the full url assigned to the key '1'?

Thanks

Aadam
aadam is offline
Reply With Quote
View Public Profile
 
Old 05-19-2010, 03:49 PM Re: Redirection regarding database
chrishirst's Avatar
Defies a Status

Posts: 44,052
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
read the database for the URL, send a redirect instruction to the useragent.
__________________
Chris. ->>
Please login or register to view this content. Registration is FREE
<<-

A foolish consistency is the hobgoblin of little minds
Thought for today:- Is SEO the only industry where all the cowboys are Indians?
chrishirst is offline
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 05-19-2010, 03:52 PM Re: How to Redirect based on page?
chrishirst's Avatar
Defies a Status

Posts: 44,052
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
Do not cross post with the same question

http://www.candsdesign.co.uk/article...filiate-links/

PHP Code -> http://www.candsdesign.co.uk/article...nks/using-php/
__________________
Chris. ->>
Please login or register to view this content. Registration is FREE
<<-

A foolish consistency is the hobgoblin of little minds
Thought for today:- Is SEO the only industry where all the cowboys are Indians?
chrishirst is offline
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 05-20-2010, 04:06 AM Re: How to Redirect based on page?
Extreme Talker

Posts: 193
Name: Adam Moseley
Trades: 0
This is part the code I am using:
Code:
if(isset($_GET['k'])) {
   $k = mysql_real_escape_string($_GET['k'], $db);
   if($result = mysql_query("SELECT `url` FROM `turl` WHERE `key` = '" . $k . "'", $db)) {
      if(mysql_num_rows($result) > 0) {
         $row = mysql_fetch_row($result);
               header('HTTP/1.1 301 Moved Permanently'); -------line 24
         header('Location: ' . $row[0]); ----------25
                exit;
      }
   }
But I get these errors below, any ideas?

Warning: Cannot modify header information - headers already sent by (output started at /home/moseleya/public_html/zumco.co.uk/index.php:9) in /home/moseleya/public_html/zumco.co.uk/index.php on line 24

Warning: Cannot modify header information - headers already sent by (output started at /home/moseleya/public_html/zumco.co.uk/index.php:9) in /home/moseleya/public_html/zumco.co.uk/index.php on line 25
__________________

Please login or register to view this content. Registration is FREE
aadam is offline
Reply With Quote
View Public Profile
 
Old 05-20-2010, 04:58 AM Re: How to Redirect based on page?
Phunk Rabbit's Avatar
Ultra Talker

Posts: 255
Name: John Nerush
Location: Milton Keynes, UK
Trades: 0
You cant send headers ( header('Location:....... ) in the middle of a script unless you are using output buffering.

It is as simple as:

PHP Code:
<?php
ob_start
();

//File contents....

ob_end_flush();
Phunk Rabbit is offline
Reply With Quote
View Public Profile Visit Phunk Rabbit's homepage!
 
Old 05-20-2010, 05:17 AM Re: How to Redirect based on page?
Extreme Talker

Posts: 193
Name: Adam Moseley
Trades: 0
I dont understand where to put that though, sorry im new to this.
__________________

Please login or register to view this content. Registration is FREE
aadam is offline
Reply With Quote
View Public Profile
 
Old 05-20-2010, 05:21 AM Re: How to Redirect based on page?
Phunk Rabbit's Avatar
Ultra Talker

Posts: 255
Name: John Nerush
Location: Milton Keynes, UK
Trades: 0
open any .php file with one (or more) of those header redirects.

Place ob_start(); on line 2, after <?php.

then place ob_end_flush(); on the 2nd from last line, followed by ?>
Phunk Rabbit is offline
Reply With Quote
View Public Profile Visit Phunk Rabbit's homepage!
 
Old 05-20-2010, 05:50 AM Re: How to Redirect based on page?
Extreme Talker

Posts: 193
Name: Adam Moseley
Trades: 0
It still delivers the same errors, just different line numbers as its moved
__________________

Please login or register to view this content. Registration is FREE
aadam is offline
Reply With Quote
View Public Profile
 
Old 05-20-2010, 05:55 AM Re: How to Redirect based on page?
Phunk Rabbit's Avatar
Ultra Talker

Posts: 255
Name: John Nerush
Location: Milton Keynes, UK
Trades: 0
can you paste your whole script?

Also, is this script being included into another file? The output buffering needs to start at the very beinging of the script.

Example:

index.php
-- included file somthing.php
-- included file redirects.php

So here, index.php includes somthing.php and somthing.php includes redirects.php, the output buffering needs to be done in index.php at line 2.
Phunk Rabbit is offline
Reply With Quote
View Public Profile Visit Phunk Rabbit's homepage!
 
Old 05-20-2010, 11:21 AM Re: How to Redirect based on page?
Extreme Talker

Posts: 193
Name: Adam Moseley
Trades: 0
This is all the script

Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>
<?php

ob_start();
 
$baseurl = 'http://zumco.co.uk/'; //address to use as the first part of the short URLs (note trailing slash)
$sql_host = 'localhost'; //host of your mysql database (usually localhost)
$sql_user = 'moseleya_moseley'; //mysql user with access to your database
$sql_pass = 'pass'; //password for this user
$sql_db = 'moseleya_turl'; //the name of your database
 
$db = mysql_connect($sql_host, $sql_user, $sql_pass);
mysql_select_db($sql_db, $db);
 
 
 
if(isset($_GET['k'])) {
         $k = mysql_real_escape_string($_GET['k'], $db);
         if($result = mysql_query("SELECT `url` FROM `turl` WHERE `key` = '" . $k . "'", $db)) {
         if(mysql_num_rows($result) > 0) {
         $row = mysql_fetch_row($result);
 header('HTTP/1.1 301 Moved Permanently');
         header('Location: ' . $row[0]);
                        exit;
      }
   } 
}
 
if(isset($_POST['submit'], $_POST['url'])) {
   if(filter_var($_POST['url'], FILTER_VALIDATE_URL, FILTER_FLAG_HOST_REQUIRED) !== false) {
      $url = mysql_real_escape_string($_POST['url'], $db);
      if($result = mysql_query("SELECT `key` FROM `turl` WHERE `url` = '" . $url . "'", $db)) {
         if(mysql_num_rows($result) > 0) {
            $row = mysql_fetch_row($result);
            $key = $row[0];
         }
      }
      if(!isset($key) && mysql_query("INSERT INTO `turl` (`url`) VALUES ('" . $url . "')", $db)) {
         $id = mysql_insert_id($db);
         $key = base_convert($id, 10, 36);
         mysql_query("UPDATE `turl` SET `key` = '" . $key . "' WHERE `id` = '" . $id . "'");
      }
      echo '<a href="' . $baseurl . $key . '" target="_blank">' . $baseurl . $key . '</a>';
             
   }
}
else {


?>
<form method="post" action="index.php">
<label>URL: <input type="text" name="url" id="url" /></label>
<br />
<input type="submit" name="submit" id="submit" value="Shorten" />
</form>
<?php
}
ob_end_flush();

?>
</body>
</html>
There are no includes its just a one page script, the site is hosted at zumco.co.uk in my sig.

Cheers
__________________

Please login or register to view this content. Registration is FREE
aadam is offline
Reply With Quote
View Public Profile
 
Old 05-20-2010, 11:27 AM Re: How to Redirect based on page?
chrishirst's Avatar
Defies a Status

Posts: 44,052
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
THIS->
Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>
Is OUTPUT, and the header() call HAS TO BE before any output.
__________________
Chris. ->>
Please login or register to view this content. Registration is FREE
<<-

A foolish consistency is the hobgoblin of little minds
Thought for today:- Is SEO the only industry where all the cowboys are Indians?
chrishirst is offline
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 05-20-2010, 11:37 AM Re: How to Redirect based on page?
Extreme Talker

Posts: 193
Name: Adam Moseley
Trades: 0
Ok, I no longer have any error messages but the page does not redirect to the full URL when visited.

Have I done something wrong in the script?

Ps I used Link for the tutorial.
__________________

Please login or register to view this content. Registration is FREE
aadam is offline
Reply With Quote
View Public Profile
 
Old 05-20-2010, 11:49 AM Re: How to Redirect based on page?
Phunk Rabbit's Avatar
Ultra Talker

Posts: 255
Name: John Nerush
Location: Milton Keynes, UK
Trades: 0
I would assume it will come down to the info in the database but without seeing it im only stabbing in the dark.
Phunk Rabbit is offline
Reply With Quote
View Public Profile Visit Phunk Rabbit's homepage!
 
Old 05-20-2010, 12:37 PM Re: How to Redirect based on page?
Extreme Talker

Posts: 193
Name: Adam Moseley
Trades: 0
Do you know if there is code to redirect a page based on its name?

I.e. If they shorten the URL and it goes to zumco.co.uk/45 can i forward that to the full url that relates to the key 45?
__________________

Please login or register to view this content. Registration is FREE
aadam is offline
Reply With Quote
View Public Profile
 
Old 05-20-2010, 12:58 PM Re: How to Redirect based on page?
Extreme Talker

Posts: 193
Name: Adam Moseley
Trades: 0
as the /xxxxx bit relates to the key in the database which in turn is associated with a full url.
__________________

Please login or register to view this content. Registration is FREE
aadam is offline
Reply With Quote
View Public Profile
 
Old 05-20-2010, 01:13 PM Re: How to Redirect based on page?
Extreme Talker

Posts: 193
Name: Adam Moseley
Trades: 0
Code:
if(isset($_GET['k'])) {
   $k = mysql_real_escape_string($_GET['k'], $db);
   if($result = mysql_query("SELECT `url` FROM `turl` WHERE `key` = '" . $k . "'", $db)) {
      if(mysql_num_rows($result) > 0) {
         $row = mysql_fetch_row($result);
         header('HTTP/1.1 301 Moved Permanently');
         header('Location: ' . $row[0]);
         exit;
That is the part of the code that is supposed to redirect the page.
Please note that turl is the table in the database that everything is located.

Where is the problem??
__________________

Please login or register to view this content. Registration is FREE
aadam is offline
Reply With Quote
View Public Profile
 
Old 05-20-2010, 01:16 PM Re: How to Redirect based on page?
chrishirst's Avatar
Defies a Status

Posts: 44,052
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
Quote:
Originally Posted by aadam View Post
Do you know if there is code to redirect a page based on its name?

I.e. If they shorten the URL and it goes to zumco.co.uk/45 can i forward that to the full url that relates to the key 45?
Script the 404 page,
extract the number from the failed URI,
get the URLfrom the database for entry 45,

redirect



Job sorted.
__________________
Chris. ->>
Please login or register to view this content. Registration is FREE
<<-

A foolish consistency is the hobgoblin of little minds
Thought for today:- Is SEO the only industry where all the cowboys are Indians?

Last edited by chrishirst; 05-20-2010 at 01:21 PM..
chrishirst is offline
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Reply     « Reply to How to Redirect based on page?

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.57027 seconds with 11 queries