Klanto Aguntuk
Klanto Aguntuk

Reputation: 719

Rediricting url dynamically from dynamically populated links

a mysql database has following three columns:

id  url_href              url
1    yahoo         http://www.yahoo.com
2    google        http://www.google.com
3    bing          http://www.bing.com

i use the following query to get the value

mysql_select_db($database_XYZ, $XYZ);
$query_urlencoder = "SELECT id, url_href, url FROM sites WHERE id >0 ORDER BY id DESC";
$urlencoder = mysql_query($query_urlencoder, $XYZ) or die(mysql_error());
$row_urlencoder = mysql_fetch_assoc($urlencoder);
$totalRows_urlencoder = mysql_num_rows($urlencoder);

the href tag

<a href="redirect.php?encodeurl=<?php echo $row_urlencoder['url']; ?>" target="_blank"><?php echo $row_urlencoder['url_href']; ?></a>

populate links on page dynamically based on repeat region like,

yahoo
google
bing

and clicking above links take the users respectively to

http://www.examplecom/redirect.php?encodeurl=http://www.yahoo.com
http://www.example.com/redirect.php?encodeurl=http://www.google.com
http://www.example.com/redirect.php?encodeurl=http://www.bing.com

but those actually aren't redirected to the respective url like

http://www.yahoo.com
http://www.google.com
http://www.bing.com

how can i redirect the href links to the the above url in this case? any suggestion or reference shall be thankfully appreciated.

Upvotes: 1

Views: 127

Answers (3)

Basic Bridge
Basic Bridge

Reputation: 1911

Try this.

<?php
while($row = mysql_fetch_assoc($urlencoder)){
echo '<a href="http://yoursite.com/redirect.php?encodeurl='.$row_urlencoder['url'].'" target="_blank">'.$row_urlencoder['url_href'].'</a>';
} ?>

In your redirect.php

<?php 

sleep(20);

header('Location:'.$_GET['encodeurl'].'');

?>

Upvotes: 1

Dipesh Parmar
Dipesh Parmar

Reputation: 27364

rawurlencode( ) will help you.

The characters that need to be escape-encoded are the control, space, and reserved characters:

; / ? : @ & = + $ ,

Delimiter characters must also be encoded:

< > # % "

The following characters can cause problems with gateways and network agents, and should also be encoded:

{} | \ ^ [ ] `

PHP provides the rawurlencode( ) function to protect them. For example, rawurlencode( ) can build the href attribute of an embedded link:

echo '<a href="search.php?q=' .
      rawurlencode("100% + more") .
      '">';

The result is an <a> element with an embedded URL correctly encoded:

<a href="search.php?q=100%25%20%2B%20more">

PHP also provides the urlencode( ) function that differs from the rawurlencode( ) function in that the former encodes spaces as a + sign whereas the latter encodes spaces as %20. The use of the + character to encode a space was an early HTTP way to encode spaces.

Upvotes: 0

Sudip
Sudip

Reputation: 2051

Use urlencode php function

redirect.php?encodeurl=<?php echo urlencode($row_urlencoder['url']); ?>

Upvotes: 1

Related Questions