Codeigniter: Redirect to another URL from view page

帅比萌擦擦* 提交于 2019-12-24 02:42:43

问题


google-php-client example calls the authorization URL when user clicks a link. I want to call it when page loads without any user click. The way it is implemented in google-client example:

$client = new Google_Client();
$client->setApplicationName('Google Contacts PHP Sample');
$client->setScopes("http://www.google.com/m8/feeds/");
$auth = $client->createAuthUrl();
print "<a class=login href='$auth'>Connect Me!</a>";

I am trying to remove dependency on clicking on Connect Me link and call url given by $client->createAuthUrl(). I am new to Codeigniter that's why struggling on this simple task.
I check that there are following different ways to call an URL but not sure which one will work here:

  1. cURL
  2. file_get_contents
  3. stream_context_create

I am using php 5.3 with XAMPP


回答1:


For a simple redirect using CodeIgniter's libraries, use the following:

$client = new Google_Client();
$client->setApplicationName('Google Contacts PHP Sample');
$client->setScopes("http://www.google.com/m8/feeds/");
$auth = $client->createAuthUrl();

$this->load->helper('url');
redirect($auth); // Returns a HTTP redirect to the client

This is a wrapper around header('Location: ... '); when no second parameter is passed (or is passed as 'location').




回答2:


Just use redirect from url helper or alternative :

echo '<script>window.location = "'.$your_location.'"</script>';



回答3:


Use the php function!

header('Location: $auth', TRUE, 301);

Since we're talking about Codeigniter, it would be more appropriate to use the built-in redirect()

 //loading the helper
 $this->load->helper('url');
 redirect($auth);

more reading on the Codeigniter URL Helper



来源:https://stackoverflow.com/questions/14542770/codeigniter-redirect-to-another-url-from-view-page

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!