Welcome Guest, Not a member yet? Register   Sign In
set a redirect with set_header?
#1

[eluser]generalsalt[/eluser]
how do you set a redirect with set_header?
#2

[eluser]vitoco[/eluser]
i do my redirects with

<?
header('location:http://www.example.com');
?>
#3

[eluser]NogDog[/eluser]
The URL helper includes a redirect() function. If you want to use set_header(), as far as I can tell it's just like using PHP's header() function:
Code:
$this->output->set_header("Location: http://www.example.com/page.php");
#4

[eluser]generalsalt[/eluser]
Hello, thanks for the response. I am seriously new to all this, so bear with me....

I have a basic else/if to check the Post variable for username/password indexes. If it finds them, I do this...

Code:
$this->load->library('session');
                $this->session->set_userdata('username', $_POST['username']);
                $this->session->set_userdata('password', $_POST['password']);

If it doesn't find them I do this...

Code:
$this->output->set_header('location: http://url.com/try_again');

But it doesn't redirect me to where I want to go, it just goes to the page that I am trying to password protect! I also tried this...

Code:
redirect('/try_again', 'refresh');

...But I get a 'headers already sent' PHP error message.

I'm reading up on all this stuff right now, If I have a eureka moment–I'll post back.
#5

[eluser]generalsalt[/eluser]
Thought I'd post my code...

Code:
function Upload()
{        
    parent::Controller();        
    $this->load->helper(array('form', 'url'));  
        
    if(isset($_POST['username']) && isset($_POST['password']))
    {
        $username = $_POST['username'];
        $password = $_POST['password'];
            
        if($username == 'doug' && $password == 'efresh')
        {
            echo 'your logged in!';
            $this->load->library('session');
            $this->session->set_userdata('username', $_POST['username']);
            $this->session->set_userdata('password', $_POST['password']);
            $username = $this->session->userdata('username');
            $password = $this->session->userdata('password');
            echo $username . "   " . $password;
         }
        else
         {
            $this->output->set_header('location: http://url.com/try_again');
         }
            
    }
    else
    {
    $this->output->set_header('location: http://url.com/try_again');
    }        
}
#6

[eluser]NogDog[/eluser]
Any PHP function that sets/modifies headers must be executed before anything is output. That "anything" includes any text - even spaces or newlines - not within <?php...?> tags. Make sure that none of the files involved in this processing has any explicit output (echo, print, etc.) or accidental output. One "gotcha" can be any included files that have a newline after the closing "?>". It is therefore recommended that you omit the closing "?>" in all class definition and other include files for this very reason.




Theme © iAndrew 2016 - Forum software by © MyBB