Jump to content

Recommended Posts

Ok I am creating my first PHP application on my own. I have worked with php for going on a year now and have altered many scrip’s. Now I am trying to take my knowledge to the next level.  I have the database working and the registration script working now I am working on the login script and cannot seem to get it to work.  I will post my registration script first so you can see how I am doing that. After that will be the login script I am not sure if I am going at this the right way but when I try to login and hit the submit button the script does not seem to run. Any help would be appreciated just please let me know what you are doing and what is going on so I can learn thanks.

 

register script (http://www.rwddesign.com/register.php)

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Register</title>

<link href="eventMain.css" rel="stylesheet" type="text/css" />

</head>

<body>
<?php

//Starts the code when form is submitted:
if( !isset($_POST['submit'])){
include_once "regform.html";
} else {
//flag variable to track success:
$okay = TRUE;

//Validate the email address:
if (empty($_POST['email1'])) {
	print '<p class="error">Please enter your email.</p>';
	$okay = FALSE;
	include_once "regform.html";
}

//Validate the password:
elseif (empty($_POST['pass1'])) {
	print '<p class="error">Please enter your password.</p>';
	$okay = FALSE;
	include_once "regform.html";
}

//Valadate legnth of password
elseif (strlen($_POST['pass1']) < 6) {
	print'<p class="error">Your password must be atleast 6 charachters long.</p>';
	$okay = FALSE;
	include_once "regform.html";
}

//validate the emails for equality:
elseif ($_POST['email1'] != $_POST['email2']) {
	print '<p class="error">Your emails do not match.</p>';
	$okay = FALSE;
	include_once "regform.html";
}

//Validate the passwords for equality:
elseif ($_POST['pass1'] != $_POST['pass2']) {
	print '<p class="error">Your passwords do not match.</p>';
	$okay = FALSE;
	include_once "regform.html";
}

//If there were no errors, print a success message:
elseif ($okay == TRUE) {

	//Trims email and password and sets to a varible:
	$email = trim($_POST['email1']);
	$password = trim($_POST['pass1']);

	//Encript password using email as salt:
	$password = sha1($email.$password);	

	//Include files for conecting to database:
	$dbc = mysql_connect('rwddesign.com:3306', 'rwddesi1_bobby31', 'jessica');
	mysql_select_db('rwddesi1_test');

	//Define the query:
	$query = "INSERT INTO users (userID, email, password) VALUES (0, '$email', '$password')";

	//Execute the query:
	if (@mysql_query($query)) {

		//Print message if secsessful
		print '<h1>You have registered</h1>';

	} else {

		//Get error number
		$errorNumber = mysql_errno();

		//print message if duplacate email
		if ( $errorNumber == 1062 ) {
			print '<h1 class="error">Email is already registered please try again.</h1>';
			include_once "regform.html";

		} else {

		//print message for all other errors.
		print '<h1 class="error">Could not register because:' . mysql_error() . ' .</h1>
			   <p class="error">The query being run was: ' . $query . '</p>';
			   include_once "regform.html";
	}
	}
	mysql_close();	
}
}

?>
</body>
</html>

 

login script (http://www.rwddesign.com/login.php)

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Login</title>

<link href="eventMain.css" rel="stylesheet" type="text/css" />

</head>

<body>

<?php 

//Starts the code when form is submitted:
if( !isset($_POST['submit'])){
include_once "loginform.html";
} else {

//Flag varable to track sucsess
$okey = TRUE;

//Check that email is not empty
if (empty($_POST['email1'])) {
	print '<p class="error">Please enter your email address.</p>';
	include_once "loginform.html";
	$okey = FALSE;


	//check that email is not empty
} elseif (empty($_POST['pass1'])) {
	print '<p class="error">Please enter your password</p>';
	include_once "loginform.html";
	$okey = FALSE;

//If there were no errors, print a success message:
} elseif ($okey == TRUE) {

	//Trims email and password and sets to a varible:
	$email = trim($_POST['email1']);
	$password = trim($_POST['pass1']);

	//Encript password using email as salt:
	$password = sha1($email.$password);	

	//Include files for conecting to database:
	$dbc = mysql_connect('rwddesign.com:3306', 'rwddesi1_bobby31', 'jessica');
	mysql_select_db('rwddesi1_test');

	//Query for checking database for user name and pass
	$loginQuery = "SELECT * FROM users WHERE email = '$email' and password = '$password'";

	//Gets results from above query
	$result = mysql_query($loginQuery);

	//Count number of rows returned from query
	$count = mysql_num_rows($result);

} elseif (@mysql_query($loginQuery)) {

	//If there is a match
	if ($count == 1) {

	// Register $myusername, $mypassword and redirect to file "login_success.php"
	//session_register("email");
	//session_register("password"); 
	include "1stCalendar.php";
	//header("location:login_success.php");
} else {
	print "Wrong Username or Password";
	include_once "loginform.html";
	}
		}

//ob_end_flush();

}

?>
</body>
</html>

Link to comment
https://forums.phpfreaks.com/topic/204123-help-creating-login-system/
Share on other sites

Here it is sorry about that.

 

<!-- This will be the login form for rwddesigns event calander. Handled by login.php -->

<form name="login" action="../login.php" method="post">

<p>
    <label for="email">Email Address:</label>
    <input type="text" name="email1" maxlength="30" size="20" value="<?php if (isset($_POST['email1'])) { print htmlspecialchars($_POST['email1']); } ?>" />
    </p>
    
    <p>
    <label for="pass1">Password:</label>
    <input type="password" name="pass1" size="22"  />
    </p>
    
    <p class="submit">
       	<input type="submit" value="Login" /> 
       	<input type="hidden" name="submit" value="true" />
    </p>
        
</form>

I dont think this has to do with your issue but you can name your submit button and check to see if the submit button has been pressed by using isset($_POST['submit']) just like you are doing with your hidden variable.

 

This will eliminate that extra step and solve some confusion in your script.

 

Can you explain exactly what is happening? Is nothing at all even attempting to load? Also where is the html file located in your directory structure?

This line doesn't seem to be of any use and looks like it shouldn't be there

 

 

 

} elseif (@mysql_query($loginQuery)) {

 

you won't get to this if you enter into the previous elseif therefore making this else if block useless since you define $loginQuery in the previous block. This is most likely ur problem. Remove the } elseif and instead use an if()

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.