I am using the following code to store my external links in a file: <?php $path = array( 'google' => 'http://www.google.com', 'yahoo'=> 'http://www.yahoo.com'); if (array_key_exists($_GET['id'], $path)) header('Location: ' . $path[$_GET['id']]); ?> Code (markup): They are called to a page by the name of the file- links.php with the id on the end. e.g. links.php?id=google What I want to know is what code can I add to the php code to make them open in a new window. thanks.
Thats not PHP, its HTML. Where you are displaying the link like: <a href="links.php?id=google.com">Visit Google</a> Code (markup): Change to: <a href="links.php?id=google.com" target="_blank">Visit Google</a> Code (markup): Peace,
I was looking for a way to do it in the php code. 1) because I would only have to use it once - rather than on every link. 2) because I think target=_blank is not valid in html 4.01
how about a little javascript? <?php $path = array( 'google' => 'http://www.google.com', 'yahoo'=> 'http://www.yahoo.com'); if (array_key_exists($_GET['id'], $path)) print "<script type='text/javascript'> window.open('" . $path[$_GET['id'] . "', new);</script>"; ?> Code (markup): Then who cares? That will only redirect users to a new page, and not the main part of the site. Common users will not be able to see the target=_blank.
I wasn't sure what you meant. I want my code to be valid. ----- I think there was something wrong with the " in the code you added: if (array_key_exists($_GET['id'], $path)) print "<script type='text/javascript'> window.open('" . $path[$_GET['id'] . "', new);</script>"; Code (markup): Parse error: syntax error, unexpected ';', expecting ']' the following error appeared when I clicked on the link.
Fixed: if (array_key_exists($_GET['id'], $path)) print "<script type='text/javascript'> window.open('" . $path[$_GET['id']] . "', new);</script>"; PHP:
Its not working at all now. The link doesn't work and it doesn't open in a new window. It just opens link.php&id=google in the browser now with a white screen which I am guessing is te link.php file.
Target blank is the only 100% success rate. Even if it is not HTML 4 complaint, everyone will keep on using it. You can add a middle page in Javascript, but then it is not 100% always the link will open (like me, users might have Javascript turned off or a pop-up blocker on (most people)). Even if you use Javascript, you are still implementing target blank, which is kinda repetitive. There is no way PHP (or server side languages) can do it. It must be client side. Peace,
Ok, thanks. That is useful to know. If I want to make the links nofollow is the best way to do it via nofollow and noindex meta tags in the link.php file?
But target="_blank" isn't valid XHTML strict code (neither transitional I think). You cannot open header(...) in a new window. You'll have to use javascript and window.open(). IF the user should have javascript disabled or using a popup blocker that blocks the window open, use this: <? if($blabla){ ?> <script type="text/javascript"> window.open(your window parametres); </script> <a href="yourlink" target="_blank">A new window should be opened, if not.. Click here.. bla bla bla</a> <? die(); //ALWAYS stop executing if there is done some parsing below, if not, it will maybe show the other output. } ?> HTML:
Even though it isn't valid, I want to use target="_blank". I don't want to use javascript. Is there a way to put the html target="_blank" into the if statement or after each link in the php file e.g 'google' => 'http://www.google.com' thanks.