views:

124

answers:

5

Hi all,

Sorry if this is blatantly obvious, but I've Googled this and I seriously cannot find any PHP-only way to do it.

Without using an HTML or Javascript - pure PHP only (from a Controller file in a CodeIgniter site) - how can I open a browser window with a link I specify?

Is it possible?

Thanks!

Jack

Edit: it seems some people are misinterpreting what I mean, I apologise for not making it clear enough. I know with PHP you can set header("Location: http://site.com") to make the browser load a new window; I wanted to know if it was possible to send a header to say "open the Location in a new window".

Edit 2: to clarify what I want to do: the user can submit something to my site. Before clicking 'Submit', they can opt (via checkbox) to Tweet about it. If the checkbox is ticked, after everything's inserted into the database etc. a new window/tab loads with the URL http://twitter.com/home?status=Hello%20World or whatever the tweet will say. The user will have opted to do this so I'm not "doing something I shouldn't". I understand in hindsight though, that there probably is a better way of doing this.

+8  A: 

You can't use a server-side language (PHP) to control client-side behavior (forcing a new browser window for a hyperlink).

BoltClock
OK, I guess that explains it then! Thanks BoltClock.
Jack Webb-Heller
Actually, I realise what I was thinking when I wrote this. You can set a header via PHP to redirect to a new page. Is there no header (or, header property or something) you can set to visit this page, but in a new window?
Jack Webb-Heller
@Jack: No. Think of what that would do - the user would click a link, and it would begin loading in the same window, and all of a sudden it would pop out into a new window. That would be Bad.
Cam
@Jack Webb-Heller: I don't believe there's such a header, not even a Web browser-specific one recognized by all browsers (browsers aren't the only HTTP clients).
BoltClock
Sending `<script type="text/javascript" language="Javascript">window.open('http://example.com');</script>` (with proper hypertext mime type) from server to the client will have browser attempt to open a new window with the url `http://example.com`, subject to popup-blocker policies etc. But again, I bet you are doing something you should not be doing, not for the good sake of your user(s) anyway.
amn
@amn I've edited the question with what I intended to do.
Jack Webb-Heller
A: 

It's not PHP but HTML that will do the trick:

<?php
echo "<a href=\"some link here\" target=\"_blank\">";
?>

or simply outside php blocks:

<a href="some link here" target="_blank">
dark_charlie
OP asked how **not** to use HTML.
BoltClock
+1  A: 

What you can do is echo, in php, the html to redirect, thats all I can think of, you have to use some other programming language.

Patrick Gates
@Patrick : I don't think *any* serverside language can accomplish this. There is really nothing you can send from the server that can cause a url to open in a new window without html/javascript, so changing your serverside language wouldn't really help.
Cam
+1  A: 

Since you are using codeigniter you can take advantage of the URL helper library. Really this just forms html though.

Docs: http://codeigniter.com/user_guide/helpers/url_helper.html

You should probably autoload the url helper in config/autoload.php or just use

 $this->load->helper('url');


 echo anchor('http://your.link.com/whatever', 'title="My News"', array('target' => '_blank', 'class' => 'new_window'));
Keyo
+1  A: 

Codeigniter has a function that may do what you want

anchor_popup()

Nearly identical to the anchor() function except that it opens the URL in a new window. You can specify JavaScript window attributes in the third parameter to control how the window is opened. If the third parameter is not set it will simply open a new window with your own browser settings. Here is an example with attributes

In the URL helper

Brad