Jump to content


Photo

Script annoying me


  • Please log in to reply
7 replies to this topic

#1 Drezard

Drezard
  • Members
  • PipPipPip
  • Advanced Member
  • 244 posts

Posted 16 September 2006 - 04:58 AM

I really cant get these two scripts to work together to give me the output i want.

Here are the two scripts:

login_form.php:

<?php
// initialize a session
session_start();
?>
<html>
<head></head>
<body>

<?php
if (!isset($_SESSION['login']) && !isset($_POST['user'])) {
    // if no data, print the form
?>
    <form action="<?php echo $_SERVER['PHP_SELF']?>" method="post">
       Username:<input type="text" name="user"><br>
	   Password:<input type="password" name="pass"><br>
        <input type="submit" name="submit">
    </form>
<?php
}
if (!isset($_SESSION['login'])) {

	include('connect.php');
    // if a session does not exist but the form has been submitted
    // check to see if the form has all required values
    // create a new session
	$user = empty($_POST['user']) ? die ("Please Enter A Username") : mysql_escape_string($_POST['user']);
	$pass = empty($_POST['pass']) ? die ("Please Enter A Password") : mysql_escape_string($_POST['pass']);
	$sql = "SELECT * FROM users WHERE user='$user' AND pass='$pass'";
	$result = mysql_query($sql);
	$count=mysql_num_rows($result);
	
	if($count==1){
		
		$_SESSION['userinfo'] = $user;	
		?>
		
		<meta http-equiv="refresh" content="0;url=login_sucess.php">;

		<?php	
	}
		
	if ($count == 0) {
		
		echo "Username or password are incorrect";

		}	
	
}
?>
</body>
</html>

login_sucess.php:

<?php
session_start();
$user = $_SESSION['username'];
setcookie('user', $user, time()+36000*24*365);
session_destroy();
?>

<head>

</head>

<body>

<?php

if (isset($_COOKIE['user'])) {
echo "Login Complete";
}
else {
echo "$user";
}
?>

</body>
</html>

Thanks, Daniel

#2 JustinMs66@hotmail.com

JustinMs66@hotmail.com
  • Members
  • PipPipPip
  • Advanced Member
  • 138 posts

Posted 16 September 2006 - 05:21 AM

...well you should probubly tell us the output you want so that we know what to help you with.

#3 markbett

markbett
  • Members
  • PipPipPip
  • Advanced Member
  • 133 posts

Posted 16 September 2006 - 05:32 AM

perhaps your problem lies:
if (isset($_COOKIE['user'])) {
echo "Login Complete";
}
else {
echo "$user";
}

how is $user set if they couldnt log in??

#4 HuggieBear

HuggieBear
  • Members
  • PipPipPip
  • Advanced Member
  • 1,899 posts
  • LocationEngland, UK

Posted 16 September 2006 - 07:09 AM

There's actually quite a bit wrong with this code, but don't worry, we can fix it :)

I'll ask you a few questions, when you know the answers they'll help you to realise the mistakes.

1. If I go to login_form.php and my cookie is already set, what happens?
2. If I go to login_form.php with no cookie set, what parts of your code are going to run?

As a side note, on login_success.php, you're setting the cookie and checking it on the same page.  That's not possible.  Until the page is reloaded (either refrshed, or revisited), the cookie won't be registered.

Regards
Huggie
Advice to MySQL users: Get phpMyAdmin and test your queries work there first, take half the hassle out of diagnosis, also check the reserved words list.

Links: PHP Docs :: RegEx's :: MySQL :: DevGuru :: w3schools

#5 Drezard

Drezard
  • Members
  • PipPipPip
  • Advanced Member
  • 244 posts

Posted 16 September 2006 - 08:37 AM

Thanks Huggie. Ill have a look at those two problems. I didnt know you couldnt check whether a cookie was set on a page after creating it.

Thanks, Daniel

#6 Drezard

Drezard
  • Members
  • PipPipPip
  • Advanced Member
  • 244 posts

Posted 16 September 2006 - 08:56 AM

Okay I changed the scripts and they started working. Except, it keep displaying "Please Enter A Username". So i changed something else and now it doesnt work at all and i tried to change the script back but it still doesnt work. I cant work out the problem. Theres no error it just doesnt display the form at all.

New login_form.php:

<?php
// initialize a session
session_start();
?>
<html>
<head></head>
<body>

<?php
if (!isset($_SESSION['userinfo']) && !isset($_POST['user']) && !isset($_COOKIE['user'])) {
    // if no data, print the form
?>
    <form action="<?php echo $_SERVER['PHP_SELF']?>" method="post">
       Username:<input type="text" name="user"><br>
	   Password:<input type="password" name="pass"><br>
        <input type="submit" name="submit">
    </form>
<?php
}
if (!isset($_SESSION['userinfo']) && !isset($_COOKIE['user'])) {

	include('connect.php');
    // if a session does not exist but the form has been submitted
    // check to see if the form has all required values
    // create a new session
	$user = empty($_POST['user']) ? die ("Please Enter A Username") : mysql_escape_string($_POST['user']);
	$pass = empty($_POST['pass']) ? die ("Please Enter A Password") : mysql_escape_string($_POST['pass']);
	$sql = "SELECT * FROM users WHERE user='$user' AND pass='$pass'";
	$result = mysql_query($sql);
	$count=mysql_num_rows($result);
	
	if($count==1){
		
		$_SESSION['userinfo'] = $user;	
		?>
		
		<meta http-equiv="refresh" content="0;url=login_sucess.php">;

		<?php	
	}
		
	if ($count == 0) {
		
		echo "Username or password are incorrect";

		}	
	
}
?>
</body>
</html>

Heres the new login_sucess.php

<?php
session_start();
$user = $_SESSION['userinfo'];
setcookie('user', $user, time()+36000*24*365);
session_destroy();
?>

<head>

</head>

<body>

<?php

if (isset($_COOKIE['user'])) {
echo "Login Complete";
}
else {
echo "$user";
}
?>

</body>
</html>

Thanks, Daniel

#7 Drezard

Drezard
  • Members
  • PipPipPip
  • Advanced Member
  • 244 posts

Posted 16 September 2006 - 12:27 PM

Please help



#8 redarrow

redarrow
  • Members
  • PipPipPip
  • Advanced Member
  • 7,308 posts
  • Locationlondon

Posted 16 September 2006 - 01:34 PM

you dont seem to use a database connection do you?

post what you got now please cheers.
Wish i new all about php DAM i will have to learn
((EMAIL CODE THAT WORKS))
http://simpleforum.ath.cx/mail2.inc
((PAYPAL INTEGRATION THAT WORKS))
http://simpleforum.a...aypal1_info.inc




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users