-1
echo '<script>alert("Your session has timeout, please relogin again!");</script>'; 
            redirect('main/login', 'refresh');

This is the error:

A PHP Error was encountered

Severity: Warning

Message: Cannot modify header information - headers already sent by (output started at /home/swtwnonu/public_html/ignite_project/application/controllers/member.php:19)

Filename: helpers/url_helper.php

Line Numer: 540

I would like to redirect the person to my main/login page, after he have clicked okay for the message that i have displayed, however i have no idea why the error happen. Please help!

castis
  • 8,154
  • 4
  • 41
  • 63
Outliers
  • 181
  • 3
  • 5
  • 15

1 Answers1

4

Redirects work using HTTP headers, which can't be sent after any output (text, HTML, invisible characters, Javascript, whatever...).

If you print a message using Javascript, you could redirect using it anyway:

echo '<script>alert("Your session has timeout, please relogin again!"); window.location.replace = \'index.php/main/login\'; window.location.href = \'index.php/main/login\';</script>';

You could also use HTML <meta /> tags to do it:

<meta http-equiv="refresh" content="0; url=index.php/main/login" />

This tag will redirect immediately after loading the website, you can change the 0 for any value (in seconds) to wait before redirecting.

Alejandro Iván
  • 3,969
  • 1
  • 21
  • 30
  • Yup it worked, even tho the error still appear by it appear for around half a second then the person will immediately get redirected! Thank you! :) – Outliers Feb 10 '15 at 01:11
  • @Outliers note that I completely removed the `redirect()` call, I'm redirecting just using Javascript and/or HTML. Any function that outputs HTTP headers will give you this error if you print anything before calling it. – Alejandro Iván Feb 10 '15 at 02:59