What happened to the Messenger Plus! forums on msghelp.net?
Shoutbox » MsgHelp Archive » Skype & Technology » Tech Talk » [FIXED]PHP script issue

[FIXED]PHP script issue
Author: Message:
YottabyteWizard
Senior Member
****

Avatar

Posts: 709
Reputation: 23
37 / Male / Flag
Joined: Mar 2005
O.P. [FIXED]PHP script issue
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&ntilde;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.

This post was edited on 06-26-2007 at 03:07 PM by YottabyteWizard.
06-26-2007 07:11 AM
Profile E-Mail PM Find Quote Report
NanaFreak
Scripting Contest Winner
*****


Posts: 1476
Reputation: 53
32 / Male / Flag
Joined: Jul 2006
RE: PHP script issue
you cant do anything with cookies once there is text on the page...
06-26-2007 07:15 AM
Profile PM Find Quote Report
YottabyteWizard
Senior Member
****

Avatar

Posts: 709
Reputation: 23
37 / Male / Flag
Joined: Mar 2005
O.P. RE: PHP script issue
So, I'll need to move setcookie(...) to another file?
06-26-2007 08:05 AM
Profile E-Mail PM Find Quote Report
WDZ
Former Admin
*****

Avatar

Posts: 7106
Reputation: 107
– / Male / Flag
Joined: Mar 2002
RE: PHP script issue
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']
06-26-2007 08:20 AM
Profile PM Web Find Quote Report
markee
Veteran Member
*****

Avatar

Posts: 1621
Reputation: 50
35 / Male / Flag
Joined: Jan 2006
RE: PHP script issue
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....
[Image: markee.png]
06-26-2007 01:28 PM
Profile PM Find Quote Report
YottabyteWizard
Senior Member
****

Avatar

Posts: 709
Reputation: 23
37 / Male / Flag
Joined: Mar 2005
O.P. RE: PHP script issue
Awesome! Thanks guys! It's working now.
06-26-2007 03:07 PM
Profile E-Mail PM Find Quote Report
« Next Oldest Return to Top Next Newest »


Threaded Mode | Linear Mode
View a Printable Version
Send this Thread to a Friend
Subscribe | Add to Favorites
Rate This Thread:

Forum Jump:

Forum Rules:
You cannot post new threads
You cannot post replies
You cannot post attachments
You can edit your posts
HTML is Off
myCode is On
Smilies are On
[img] Code is On