PHP - 在新窗口中打开链接?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3453473/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 09:49:25  来源:igfitidea点击:

PHP - open a link, in a new window?

phpcodeigniter

提问by Hyman

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

对不起,如果这很明显,但我已经用谷歌搜索了这个,我真的找不到任何只用 PHP 的方法来做到这一点。

Withoutusing 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?

使用 HTML 或 Javascript - 仅纯 PHP(来自 CodeIgniter 站点中的控制器文件) - 如何使用我指定的链接打开浏览器窗口?

Is it possible?

是否可以?

Thanks!

谢谢!

Hyman

Hyman

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 Locationin a new window".

编辑:似乎有些人误解了我的意思,我很抱歉没有说清楚。我知道使用 PHP 可以设置header("Location: http://site.com")让浏览器加载一个新窗口;我想知道是否可以发送标题以说“Location在新窗口中打开”。

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%20Worldor 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.

编辑 2:澄清我想做什么:用户可以向我的网站提交一些内容。在点击“提交”之前,他们可以选择(通过复选框)发布关于它的推文。如果选中该复选框,则在将所有内容插入数据库等之后,会加载一个带有 URLhttp://twitter.com/home?status=Hello%20World或推文将要说的任何内容的新窗口/选项卡。用户将选择这样做,所以我不会“做我不应该做的事情”。不过,我事后明白,可能有更好的方法来做到这一点。

回答by BoltClock

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

您不能使用服务器端语言 (PHP) 来控制客户端行为(强制使用新的浏览器窗口获取超链接)。

回答by Brad

Codeigniter has a function that may do what you want

Codeigniter 有一个功能可以做你想做的

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

除了它在新窗口中打开 URL 之外,与 anchor() 函数几乎相同。您可以在第三个参数中指定 JavaScript 窗口属性来控制窗口的打开方式。如果未设置第三个参数,它将简单地使用您自己的浏览器设置打开一个新窗口。这是一个带有属性的示例

In the URL helper

在 URL 助手中

回答by Keyo

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

由于您使用的是 codeigniter,因此您可以利用 URL 帮助程序库。真的这只是形成html。

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

文档:http: //codeigniter.com/user_guide/helpers/url_helper.html

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

您可能应该在 config/autoload.php 中自动加载 url helper 或仅使用

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


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

回答by Chirag Rafaliya

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

您可以使用纯 html 或 codeigniter url helper 如下......

  1. open a link in htmlway..

  2. using php Codeigniter, to open in new Tab

  3. using php Codeigniter, to open in new Window

  1. html方式打开链接..

  2. 使用 php Codeigniter,在新标签页中打开

  3. 使用 php Codeigniter,在新窗口中打开

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

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

回答by MartinB

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

我知道这是一个非常古老的帖子,但我最近只是在搜索这个确切的东西。

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

Codeigniter有一个与anchor()调用非常相似的函数,anchor_popup()它在新窗口中打开目标。

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

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

回答by caffeine

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

鉴于您有:

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

在“新闻”控制器中,您有:

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

      redirect(.$url); 
 } 
      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

在此示例中,“目标空白”强制 url 在新选项卡中打开。如果需要,您可以指定新窗口。我希望这有帮助

回答by Karel Petranek

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

不是 PHP 而是 HTML 可以解决问题:

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

or simply outside php blocks:

或者只是在 php 块之外:

<a href="some link here" target="_blank">

回答by Patrick Gates

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.

你能做的是echo,在php中,重定向的html,这就是我能想到的,你必须使用其他一些编程语言。