[FIXED]PHP script issue - Printable Version
-Shoutbox (https://shoutbox.menthix.net)
+-- Forum: MsgHelp Archive (/forumdisplay.php?fid=58)
+--- Forum: Skype & Technology (/forumdisplay.php?fid=9)
+---- Forum: Tech Talk (/forumdisplay.php?fid=17)
+----- Thread: [FIXED]PHP script issue (/showthread.php?tid=75658)
[FIXED]PHP script issue by YottabyteWizard on 06-26-2007 at 07:11 AM
Well, I'm still a newbie programming PHP... but I already read several ebooks and another book I have.... still unable to fix this issue.
What I'm trying to do is login system, but the cookie is unable to be set.
I tried writting the code on a separate file, and it doesn't work.
This is the code I currently have (named usuario.php)
code: <?php
error_reporting(E_ALL);
require('./inc/connect.php');
echo ("<br /> Webaction: ".$webaction."<br />");
echo ($_COOKIE['lagoweb[username]']);
//Desloguea al usuario
if ($webaction == "logout") {
echo ("Testing logout");
setcookie('lagoweb[username]');
}
//Procesa la forma de login
elseif ($webaction == "login") {
if (isset($_COOKIE['lagoweb[username]'])) {
echo "You are already logged in!, do you want to <a href=usuario.php?webaction=logout>logout</a>?";
} else {
echo ("<form method='POST' class='loginform'><table><tr><td>Usuario/td><td><input type='text' name='username'></td></tr><tr><td>Contraseña/td><td><input type='password' name='password'></td></tr><tr><td><input type='submit'></td></tr><tr><td><input type='hidden' value='do_login' name='webaction'></td></tr>
</table></form>");
}
}
//Proceso de inicio de sesion
elseif ($webaction == "do_login") {
$dbquery = mysql_query("SELECT * FROM users WHERE username = '$username' AND password = '$password'") or die (mysql_error);
if (mysql_num_rows($dbquery) == 1) {
setcookie("lagoweb[username]",$username,time()+3600);
echo "Success!";
} else {
echo "Failed";
}
}
?>
Any help will be greatly appreciated.
Thanks in advance.
RE: PHP script issue by NanaFreak on 06-26-2007 at 07:15 AM
you cant do anything with cookies once there is text on the page...
RE: PHP script issue by YottabyteWizard on 06-26-2007 at 08:05 AM
So, I'll need to move setcookie(...) to another file?
RE: PHP script issue by WDZ on 06-26-2007 at 08:20 AM
Just make sure you set your cookies before you output anything. So no echo lines before your setcookie() lines. Alternatively, you could use output buffering.
Also, this code won't work as expected...
$_COOKIE['lagoweb[username]']
PHP will create an array based on that cookie name, so you should use...
$_COOKIE['lagoweb']['username']
RE: PHP script issue by markee on 06-26-2007 at 01:28 PM
Worst case scenario, if you have no way but to have text output before having to set the cookie (I really don't see why that would be the case, but it is a purely hypothetical situation I am dealing with anyway) you can always use javascript to set the cookie....
RE: PHP script issue by YottabyteWizard on 06-26-2007 at 03:07 PM
Awesome! Thanks guys! It's working now.
|