php links file open in a new window.

Discussion in 'PHP' started by fadetoblack22, Nov 26, 2008.

  1. #1
    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.
     
    fadetoblack22, Nov 26, 2008 IP
  2. Barti1987

    Barti1987 Well-Known Member

    Messages:
    2,703
    Likes Received:
    115
    Best Answers:
    0
    Trophy Points:
    185
    #2
    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,
     
    Barti1987, Nov 26, 2008 IP
  3. fadetoblack22

    fadetoblack22 Well-Known Member

    Messages:
    2,399
    Likes Received:
    62
    Best Answers:
    0
    Trophy Points:
    160
    #3
    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
     
    fadetoblack22, Nov 27, 2008 IP
  4. djzmo

    djzmo Active Member

    Messages:
    165
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    58
    #4
    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.
     
    djzmo, Nov 27, 2008 IP
  5. fadetoblack22

    fadetoblack22 Well-Known Member

    Messages:
    2,399
    Likes Received:
    62
    Best Answers:
    0
    Trophy Points:
    160
    #5
    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.
     
    fadetoblack22, Nov 27, 2008 IP
  6. djzmo

    djzmo Active Member

    Messages:
    165
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    58
    #6
    Fixed:
    
    if (array_key_exists($_GET['id'], $path)) print "<script type='text/javascript'> window.open('" . $path[$_GET['id']] . "', new);</script>";
    
    PHP:
     
    djzmo, Nov 27, 2008 IP
  7. fadetoblack22

    fadetoblack22 Well-Known Member

    Messages:
    2,399
    Likes Received:
    62
    Best Answers:
    0
    Trophy Points:
    160
    #7
    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.
     
    fadetoblack22, Nov 27, 2008 IP
  8. Barti1987

    Barti1987 Well-Known Member

    Messages:
    2,703
    Likes Received:
    115
    Best Answers:
    0
    Trophy Points:
    185
    #8
    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,
     
    Barti1987, Nov 27, 2008 IP
  9. fadetoblack22

    fadetoblack22 Well-Known Member

    Messages:
    2,399
    Likes Received:
    62
    Best Answers:
    0
    Trophy Points:
    160
    #9
    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?
     
    fadetoblack22, Nov 27, 2008 IP
  10. fadetoblack22

    fadetoblack22 Well-Known Member

    Messages:
    2,399
    Likes Received:
    62
    Best Answers:
    0
    Trophy Points:
    160
    #10
    Is it not possible to put rel="nofollow" after 'google' => 'http://www.google.com', ??
     
    fadetoblack22, Dec 1, 2008 IP
  11. elias_sorensen

    elias_sorensen Well-Known Member

    Messages:
    852
    Likes Received:
    20
    Best Answers:
    0
    Trophy Points:
    110
    #11
    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:
     
    elias_sorensen, Dec 1, 2008 IP
  12. fadetoblack22

    fadetoblack22 Well-Known Member

    Messages:
    2,399
    Likes Received:
    62
    Best Answers:
    0
    Trophy Points:
    160
    #12
    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.
     
    fadetoblack22, Dec 1, 2008 IP