Jack
Jack

Reputation: 9784

PHP - open a link, in a new window?

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!

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://example.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.

Upvotes: 3

Views: 60323

Answers (8)

MartinB
MartinB

Reputation: 11

I know this is a really old post but I was recently just search this exact thing.

Codeigniter has a function very similar to anchor() called anchor_popup() which opens the target in a new window.

http://www.codeigniter.com/user_guide/helpers/url_helper.html?highlight=anchor_popup#anchor_popup

Upvotes: 1

Chirag Rafaliya
Chirag Rafaliya

Reputation: 209

you can use pure html or codeigniter url helper as below......

  1. open a link in html way..

  2. using php Codeigniter, to open in new Tab

  3. using php Codeigniter, to open in new Window

$attributes = { 'width' => '800', 'height' => '600', 'scrollbars' => 'yes', 'status' => 'yes', 'resizable' => 'yes' }


1. <a href="http://www.google.com" target="_blank">Google</a>

2. <?php echo anchor(prep_url('www.google.com'), 'Google', 'target="_blank"'); ?>

3. <?php echo anchor_popup(prep_url('www.google.com'), 'Google', '$attributes'); ?>

Upvotes: 3

caffeine
caffeine

Reputation: 11

You can accomplish this in view, in the "anchor" that brings visitors to the controller function that does redirection.

Example:

In view you have:

  anchor('news/external_link/'.$link['id'], 'target="_blank"');

In "news" controller you have:

  function external_link($id)
  {
     $url = $this->model->urls_model->get_url($id);

      redirect(.$url); 
 } 

In this example, "target blank" forces the url to open in a new tab. You can specify new window instead if you want. I hope this helps

Upvotes: 1

Brad
Brad

Reputation: 1691

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

Upvotes: 5

Benbob
Benbob

Reputation: 14254

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'));

Upvotes: 4

Patrick Gates
Patrick Gates

Reputation: 391

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.

Upvotes: 1

Karel Petranek
Karel Petranek

Reputation: 15154

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">

Upvotes: 1

BoltClock
BoltClock

Reputation: 723598

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

Upvotes: 10

Related Questions