Forum Moderators: coopster

Message Too Old, No Replies

php redirecting and cookie

         

ycliuwz

8:25 am on May 17, 2005 (gmt 0)

10+ Year Member



hi there,
could anyone help me out with page redirecting.
currently i have this login page, when user login , if valid , it'll redirect to specific page of user.
if not valid return prompt and redirect back to login page.

i tried using header("Location: #*$!"); but it don't seem to work.

also when after checking user login. if true, setcookie('username', $_POST['username']) or die('Could not set login cookie');

but then it'll return something like warning: header...

how can i go fixing this? please advice. thanks

mcibor

8:42 pm on May 17, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Warning: Headers already sent means that you are sending something to user before setting the cookie or using header(). If your header doesn't succeed you can always add a javascript redirect (it won't mix things up, because if php header works, then there's no time for js redirect to work.

The code for js redirect is:
<body onload="javascript: self.location.href = 'index.html';">
</body>

Hope this helps
Michal Cibor

PS. Also about php headers see if you don't have any blank lines before <?php

StupidScript

9:11 pm on May 17, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



header()
and
setcookie()
are both included in HTTP header information and have the same simple limitation noted by mcibor: there must not be ANY output before invoking either of them.

These will cause the error you noted:

<spacebar>

<?php

header("Location: [new location]");

and

<html>

<?php

setcookie("cookieName","cookieValue");

There must be absolutely nothing output before invoking a header() or setcookie() function ... not even a carriage return. Any script that uses header() or setcookie() should start at the first line of code on the page, and the script must not produce any output before those two functions, so no "echo"s or "print"s or anything.

In addition, once you (successfully) set some HTTP header info using the header() function, you want to kill the rest of the page code by exitting out of the script:

header("Location: [new location]");

exit;

Otherwise, while the header() is doing its 302 redirect thing, the rest of the script will continue to run, leading to possibly undesireable side effects.

ycliuwz

12:56 am on May 18, 2005 (gmt 0)

10+ Year Member



hi Michal,
hmm...in that case, how can i implement .js into php?

etc.
after setting cookies..
i would like to redirect the user to their specific page? how can i do that? thanks

i guess this won't work
setcookie(..);
setcookie(..);
header('Location:..');

mcibor

12:11 pm on May 18, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



<head>
<script type="text/javascript>
<!--
function fixDate(date) {
var base = new Date(0);
var delay = base.getTime();
if (delay > 0)
date.setTime(date.getTime() - delay);
}
function setCookie(name, value, expires, path, domain, secure) {
var varCookie = name + "=" + escape(value) +
((expires)? "; expires=" + expires.toGMTString() : "") +
((path)? "; path=" + path : "") +
((domain)? "; domain=" + domain : "") +
((secure)? "; secure" : "");
document.cookie = varCookie;
}

function getCookie(name) {
var varCookie = document.cookie;
var prefix = name + "=";
var begin = varCookie.indexOf(prefix);
if (begin == -1)
return null;
var end = varCookie.indexOf(";", begin);
if (end == -1)
end = varCookie.length;
return unescape(varCookie.substring(begin + prefix.length, end));
}

function deleteCookie(name, path, domain) {
if (getCookie(name)) {
document.cookie = name + "=" +
((path)? "; path=" + path : "") +
((domain)? "; domain=" + domain : "") +
"; expires=Thu, 01-Jan-70 00:00:01 GMT";
}
}

function my_function()
{
var now = new Date();
fixDate(now);
now.setTime(now.getTime() + 365 * 24 * 60 * 60 * 1000);
setCookie("login", "<?php echo $username;?>", now);
self.location.href = 'index.html';
}
//-->
</script>
</head>
<body onload="my_function()">

The first four functions can be in another file, the my_function has to be in the html or php, as it needs to be processed by php.
This code will setcookie and then redirect, however I would still work on a solo php solution.
Best regards
Michal Cibor

StupidScript

6:42 pm on May 18, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Have you tried redirecting first and then setting the cookie(s) on that page?

<?php

if ([condition-is-met]) {

$new_page_uri="http ://yadda.com/page.php?".$QUERY_STRING;

header("Location: $new_page_uri");

}

?>

and on $new_page_uri:

<?php

if ([condition-is-met]) {

setcookie(yaddayadda);

...

?

ycliuwz

12:35 am on May 19, 2005 (gmt 0)

10+ Year Member



hmm...how about using sessions instead of cookies?is it done the same way as how we setcookie? replace with startsession()?
cause from what i found, session is more secure than cookies.

mcibor

9:15 am on May 19, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



session is more secure than cookies, because all the data is stored on the server side.
to operate with sessions you need to start every file with
start_session();
and then use $_SESSION variable to pass between pages.

More about sessions you will find on [pl2.php.net...]

Best regards
Michal Cibor

ycliuwz

1:03 am on May 20, 2005 (gmt 0)

10+ Year Member



Yupz, changed to session but i encounter this problem.
when a valid user log in.

<?php
session_start()

..
.
if(checkLogin($_POST['username'],md5($_POST['password'])))
{
$_SESSION['validuser'];
session_register('validuser');
header(Location: redirect);
}
..

so after redirecting to page2.php
<?php
session_start()
$user = $_SESSION['validuser'];
..
echo 'Welcome $user';
// can't display $user?

how can should i do this thanks.

mcibor

9:00 pm on May 20, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



<?php
session_start();
if(checkLogin($_POST['username'], md5($_POST['password'])))
{
$_SESSION['validuser']= $_POST['username'];
header(Location: redirect);
}
?>

After redirecting to page2.php

<?php
session_start();
$user = $_SESSION['validuser'];
echo 'Welcome $user';
?>

You simply forgot to write the user to session, that's all.

If it still doesn't work do bother to write
Michal Cibor