View Full Version : help with code


nubs
01-12-2006, 01:43 AM
i need help placing a welcome message on my members area of my website ....something like welcome to my site "username" ......how do i go about doing this

here is my login script:


<body bgcolor="#000000" text="#FFFFFF" link="#FF0000">
<img src="pics/drinkyloggedin.gif" width="144" height="163"><?php
ob_start();

include("config.php");

// connect to the mysql server
$link = mysql_connect($server, $db_user, $db_pass)
or die ("Could not connect to mysql because ".mysql_error());

// select the database
mysql_select_db($database)
or die ("Could not select database because ".mysql_error());

$match = "select id from $table where username = '".$_POST['username']."'
and password = '".$_POST['password']."';";

$qry = mysql_query($match)
or die ("Could not match data because ".mysql_error());
$num_rows = mysql_num_rows($qry);

if ($num_rows <= 0) {
echo "Sorry, there is no username $username with the specified password.<br>";
echo "<a href=login.html>Try again</a>";
exit;
} else {

setcookie("loggedin", "TRUE", time()+(3600 * 24));
setcookie("mysite_username", "$username");
echo "You are now logged in!<br>";
echo "Continue to the <a href=members.php>members</a> section.";
}
ob_end_flush();
?>

Can someone Help Please and thank you !!

Douglas
01-12-2006, 07:59 PM
This won't work, because of where the setcookie function is, I fixed it up a bit, but basically, the setcookie function must be at the very top of page, before any text or whitespace:


<body bgcolor="#000000" text="#FFFFFF" link="#FF0000">
<img src="pics/drinkyloggedin.gif" width="144" height="163" />
<?php
ob_start();

include("config.php");

// connect to the mysql server
$link = mysql_connect($server, $db_user, $db_pass)
or die ("Could not connect to mysql because ".mysql_error());

// select the database
mysql_select_db($database);

$match = "SELECT id FROM $table WHERE username = '".$_POST['username']."'
and password = '".$_POST['password']."'";

$qry = mysql_query($match)
or die ("Could not match data because ".mysql_error());
$num_rows = mysql_num_rows($qry);

if ($num_rows <= 0) {
echo "Sorry, there is no username $username with the specified password.<br>";
echo "<a href=\"login.html\">Try again</a>";
exit;
}
else {
setcookie("loggedin", "TRUE", time()+(3600 * 24));
setcookie("mysite_username", "$username");
echo "You are now logged in!<br>";
echo "Continue to the <a href=members.php>members</a> section.";
}
ob_end_flush();
?>

nubs
01-12-2006, 08:52 PM
so i cant put a login welcome to the user on my site?

Douglas
01-12-2006, 08:58 PM
Well, you can just submit the login form to a different page so you can use cookeis :0

nubs
01-12-2006, 09:19 PM
would somthing like this work?

$mysite_username = $HTTP_COOKIE_VARS["mysite_username"];
echo "you are logged in as $mysite_username";

Douglas
01-12-2006, 09:55 PM
No, it doesn't work like sessions, you have to use one of the setcookie functions, submit the form to a file called login2.php or something, then do all the checking and stuff there, without any text, or header() functions, then use the setcookie when needed :)