Forum Moderators: coopster
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
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
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.
<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
More about sessions you will find on [pl2.php.net...]
Best regards
Michal Cibor
<?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.
<?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';
?>
If it still doesn't work do bother to write
Michal Cibor