More PHP help [FIX'D]

Subscribe to More PHP help [FIX'D] 7 posts

Sign in to reply


 
avatar for SavageWolf SavageWolf 2884 posts
Flag Post

I have this script to log someone in using cookies:


<?php
require("../_cookie.php");

$name = $_POST["name"];
$password = $_POST['password'];

//Connect;

$con = mysql_connect("localhost:Database","savagew_savagew","...");
if (!$con){
    die('Could not connect: ' . mysql_error());
}

//Select the db;

mysql_select_db("savagew_Database", $con);

$result = mysql_query("SELECT * FROM Users WHERE Name = '$name'");

//Store in arrays;

while($row = mysql_fetch_array($result)){
	if(---($password) == $row['Password']){
		$loginSuccess = true;
	}else{
		$fail[] = "Error 1: Wrong Password";
	};
};

if($loginSuccess){//time()+(60*60*24*7)
   setcookie("username", $name, 500000000000000, "/");
   setcookie("password", ---($password), 500000000000000000000, "/");
};

$i = 0;
$failMessage = "Errors:";
while(true){
    if($fail[$i] == null){
		break;
    };
    $failMessage .= "+".$fail[$i];
    $i ++;
};
if($failMessage == "Errors:"){
	header('Location: '.$_SERVER["HTTP_REFERER"]);
}else{
    passToJS("from", $_SERVER["HTTP_REFERER"]);
    passToJS("fail", $failMessage);
};
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<?php require("../_head.php")?>
<title>[LOGIN]</title>
</head>

<body>

<script language="JavaScript">
	if(fail != "Errors:"){
		fail = fail.split("+");
		totalFail = ""
		for(var i in fail){
			totalFail += "\n" + fail[i];
		};
		alert(totalFail);
    };
    if(from == ""){
		window.location = "http://www.wolfthatissavage.com/home.php";
    }else{
		window.location = from;
    };
</script>
</body>

</html>

And this to load data:


<?php
$forbid = false;
//Connect;

$con = mysql_connect("localhost:Database","savagew_savagew","...");
if (!$con){
	die('Could not connect: ' . mysql_error());
};
	
//Select the db;
	
mysql_select_db("savagew_Database", $con);

$userName = $_COOKIE["username"];
$password = $_COOKIE["password"];
echo $userName."!!";

$result = mysql_query("SELECT * FROM Users WHERE Name = '$userName'");

while($row = mysql_fetch_array($result)){
	if($password == $row['Password']){
		$loginSuccess = true;
	}else{
		$forbid = true;
	};
};

if(isset($_COOKIE["username"]) && !$forbid){
	$loggedIn = true;

	$result = mysql_query("SELECT * FROM Users WHERE Name = '$userName'");

	//Store in arrays;

	while($row = mysql_fetch_array($result)){
		$userID = $row['ID'];
		$userisMod = $row['isMod'];
		$userisAdmin = $row['isAdmin'];
		$userisBot = $row['isBot'];
	};
}else{
	$loggedIn = false;
};
?>

Unfortunately, the username cannot be retrieved by it, returning $userName as null.

 
avatar for Pimgd Pimgd 1655 posts
Flag Post

And $_COOKIE[“username”]? Can that even be retrieved?
If not, is there even a cookie?
What’s the expiration date of it?

 
avatar for SavageWolf SavageWolf 2884 posts
Flag Post

 
avatar for Pimgd Pimgd 1655 posts
Flag Post

soooo… Uh, I don’t really know.
I suppose, you should start with a tiny test script that opens a cookie. Get it to print stuff on screen. Copy from some tutorial if you have to. Then copy the solution into your code.

 
avatar for SavageWolf SavageWolf 2884 posts
Flag Post

I already have some cookies working…

 
avatar for SavageWolf SavageWolf 2884 posts
Flag Post

Hooray!

Turns out the form was sending data to http://wolfthatissavage.com rather than http://www.wolfthatissavage.com!

 
avatar for Pimgd Pimgd 1655 posts
Flag Post

Yay!
Add [FIXED] to the title.
- Try isolating the piece of code that isn’t working, then try to make it work in a controlled enviroment.

Sign in to reply