AV Posted September 15, 2007 Share Posted September 15, 2007 Hello, I have been playing around with a script which makes that when you click link, it goes to: localhost/redirect/123 where 123 is ID of an article. After that it redirects you to the outside link. I have created this: if(isset($_GET['action'])) { if($_GET['id']) { $id = (int)$_GET['id']; $query = mysql_query("SELECT * FROM news WHERE id = '$id'"); while($row = mysql_fetch_array($query)) { $insert = "UPDATE news SET views = views + 1"; mysql_query($insert); header("Location:". $row['url']); } } } //[...] echo "<a href=\"{$???}\"></a> Problem is I have no idea how I can finish that script. Can someone help me? Quote Link to comment Share on other sites More sharing options...
rarebit Posted September 15, 2007 Share Posted September 15, 2007 How do you mean finish the script? Quote Link to comment Share on other sites More sharing options...
Jessica Posted September 15, 2007 Share Posted September 15, 2007 Are you already using mod_rewrite to make that url go to a page which has $id=123? Quote Link to comment Share on other sites More sharing options...
AV Posted September 15, 2007 Author Share Posted September 15, 2007 rarebit, I want to do script like in example, you click e.g. link1 and it goes to localhost/redirect/123 where 123 is id of link1 in database jesirose, no I do not. Guy I get that code from says itsn't needed. Quote Link to comment Share on other sites More sharing options...
rarebit Posted September 15, 2007 Share Posted September 15, 2007 Script should work, what's the error, what's happening??? Quote Link to comment Share on other sites More sharing options...
AV Posted September 15, 2007 Author Share Posted September 15, 2007 It should however I don't what is this for: if(isset($_GET['action'])){... and what variable put in the echo "<a href=\"{$???}\"></a> Quote Link to comment Share on other sites More sharing options...
darkfreaks Posted September 15, 2007 Share Posted September 15, 2007 should be <?php print "<a href="$???"></a>";?> Quote Link to comment Share on other sites More sharing options...
rarebit Posted September 15, 2007 Share Posted September 15, 2007 The 'isset' is checking for a GET variable, probably from a submit button from a form. And for the other variable, put in any address you want, it's just something to display if the submit button wasn't used to access the page. Quote Link to comment Share on other sites More sharing options...
AV Posted September 15, 2007 Author Share Posted September 15, 2007 But I don't have any submit button. My 'button' is link. darkfreaks, that isn't important, I want to know how can I finish script Quote Link to comment Share on other sites More sharing options...
rarebit Posted September 15, 2007 Share Posted September 15, 2007 What's calling the page then? Quote Link to comment Share on other sites More sharing options...
darkfreaks Posted September 15, 2007 Share Posted September 15, 2007 <?php print "<a href="localhost/redirect/$id"></a>";?> Quote Link to comment Share on other sites More sharing options...
AV Posted September 15, 2007 Author Share Posted September 15, 2007 darkfreaks, it doesn't work, when I click on link I get on localhost/redirect/ ,not localhost/redirect/$id Maybe something wrong with this? if(isset($_GET['action'])) { if($_GET['id']) { Quote Link to comment Share on other sites More sharing options...
rarebit Posted September 15, 2007 Share Posted September 15, 2007 Well if you've got no button then you won't have either of those variables set! Quote Link to comment Share on other sites More sharing options...
darkfreaks Posted September 15, 2007 Share Posted September 15, 2007 try <?php if(isset($_GET['action']||$_GET['id'])) { $id = (int)$_GET['id']; $query = mysql_query("SELECT * FROM news WHERE id = '$id'"); while($row = mysql_fetch_array($query)) { $insert = "UPDATE news SET views = views + 1"; mysql_query($insert); header("Location:". $row['url']); } } ?> Quote Link to comment Share on other sites More sharing options...
rarebit Posted September 15, 2007 Share Posted September 15, 2007 Are you calling the page with something like this: www.mysite.com/index.html?action=redirect&id=123 Quote Link to comment Share on other sites More sharing options...
AV Posted September 15, 2007 Author Share Posted September 15, 2007 Here is whole code btw: <?php //redirect script if(isset($_GET['id'])) {$id = (int)$_GET['id']; $query = mysql_query("SELECT * FROM news WHERE id = '$id'"); while($row = mysql_fetch_array($query)) { $insert = "UPDATE news SET views = views + 1"; mysql_query($insert); header("Location:". $row['url']); } } //redirect script end $row = mysql_num_rows($result); for ($i=0;$i<$row;$i++) { $array = mysql_fetch_array($result); print "<td><a href=\"/redirect/$id\">{$array["title"]}</a>"; } ?> rarebit, I try to call function by localhost/redirect/ ,your method doesn't work. darkfreaks, same as before :/ Quote Link to comment Share on other sites More sharing options...
rarebit Posted September 15, 2007 Share Posted September 15, 2007 From my suggestion add everything from the ? to the end of yours (inc the ?). That's the GET bit! localhost/redirect?action=redirect&id=123 Quote Link to comment Share on other sites More sharing options...
Jessica Posted September 15, 2007 Share Posted September 15, 2007 Without modrewrite, you cannot do that. Quote Link to comment Share on other sites More sharing options...
AV Posted September 15, 2007 Author Share Posted September 15, 2007 Oh, how may I use that mod then? Any tutorials or examples? Quote Link to comment Share on other sites More sharing options...
darkfreaks Posted September 15, 2007 Share Posted September 15, 2007 i believe jesi posted a link on page one if you look Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.