...

View Full Version : Dynamic links help



name _F1
01-28-2007, 09:55 AM
I'm trying to create some dynamic links that appear in a list of links. The user specifies the title of the link, the href and whether or not to load the link in an IFRAME.

I am saving the full code for the links as a cookie (in this way I can reduce what would be 9 cookies to 3, as the user can select up to 3 dynamic links).

When the cookies are displayed in the navigation menu, the attributes of the <a> tags are messed up, as the quotation marks are escaped. I'll show you the code of the pages that set the cookies:



<?php

if (isset($_GET["action"])) { $action = $_GET["action"]; } else { $action = "base"; }

function custom() {
echo "<div class='box'>";
echo "<form action='?action=save&save=custom' method='post'>";
echo "<table cellspacing='1' cellpadding='2'><tr><td>";
echo "Custom Link 1";
echo "</td><td>";
echo "<input type='text' value='Title' name='cname1' id='cname1' onfocus=\"this.select()\" />";
echo "</td><td>";
echo "<input type='text' value='http://' name='clink1' id='clink1' />";
echo "</td><td>";
echo "<label for='ib1'>Use iFrame</label> <input type='checkbox' name='ib1' id='ib1' />";
echo "</td></tr>";
echo "<tr><td>";
echo "Custom Link 2";
echo "</td><td>";
echo "<input type='text' value='Title' name='cname2' id='cname2' onfocus=\"this.select()\" />";
echo "</td><td>";
echo "<input type='text' value='http://' name='clink2' id='clink2' />";
echo "</td><td>";
echo "<label for='ib2'>Use iFrame</label> <input type='checkbox' name='ib2' id='ib2' />";
echo "</td></tr>";
echo "<tr><td>";
echo "Custom Link 3";
echo "</td><td>";
echo "<input type='text' value='Title' name='cname3' id='cname3' onfocus=\"this.select()\" />";
echo "</td><td>";
echo "<input type='text' value='http://' name='clink3' id='clink3' />";
echo "</td><td>";
echo "<label for='ib3'>Use iFrame</label> <input type='checkbox' name='ib3' id='ib3' />";
echo "</td><tr><td colspan='2'>";
echo "<input type='submit' value='Save Settings' />";
echo "<input type='button' value='Cancel' onclick='history.go(-1);' />";
echo "</td></tr></table>";
echo "</form></div>";
}

function save() {
if ($_GET["save"] == custom) {
for ($i=0;$i<=3;$i++) {
if ($_POST["cname".$i] != "Title" && isset($_POST["clink".$i]) && $_POST["clink".$i] != 'http://' ) {
$cname = $_POST["cname$i"];
$clink = $_POST["clink$i"];
if (isset($_POST["ib".$i])) {
$function = "top.loadIframe('$clink')";
setcookie("custom$i","<a href='#' onclick='$function'>$cname</a>",time()+157680000);
}
else {
setcookie("custom$i","<a href='$clink'>$cname</a>",time()+157680000);
}
}
}
}
}

function showMessage() {
echo "<div class='box'>";
echo "<p>Settings saved successfully.</p>";
echo "<p><a href='settings.php'>Return to settings</a></p>";
echo "</div>";
}

if ($action == "save") { save(); }

?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
</head>

<body>
<?php

if (isset($_GET["action"])) { $action = $_GET["action"]; } else { $action = "base"; }

if ($action == "custom") { custom(); }
if ($action == "save") { showMessage(); }

?>
</body>
</html>

The code used to display the links:


<?php

for ($i=0;$i<=3;$i++) {
if (isset($_COOKIE["custom".$i])) {
$custom = $_COOKIE["custom".$i];
echo " $custom";
}
}

?>

Which outputs the following (I have set the titles and hrefs to Yahoo and Google, Yahoo is supposed to load in iFrame while Google is not):


<a href=\'http://www.google.com/\'>Google</a> <a href=\'#\' onclick=\'top.browserURI(\'http://www.yahoo.com/\')\'>Yahoo!</a>

The escaped quotes mean that instead of linking to Google, that link goes to http://domain.com/folder/\'http://www.google.com/\' and the link to Yahoo goes to http://domain.com/folder/\'#/\'

Any help or ideas would be appreciated, because I am baffled as to how I can resolve this problem.

koyama
01-28-2007, 11:04 AM
In that case you must be having magic_quotes on. How about this:


$custom = stripslashes($_COOKIE["custom".$i]);

name _F1
01-28-2007, 12:22 PM
Thank you; that works.:thumbsup:



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum