Jump to content

Recommended Posts

function login()
{	
	$db_hostname = "localhost"; 
	$db_username = "root";
	$db_password = "";
	$db_name =     "justinalba_module3_db";
	$dbc = @mysqli_connect($db_hostname, $db_username, $db_password, $db_name)
   	 or die("Unable to connect to MySQL");
	if (isset($_POST['submitLogin'])) 
	{ 
// if form has been submitted
// makes sure they filled it in
 		if(!$_POST['userName'] | !$_POST['passwordLogin']) 
		{
 			die('You did not fill in a required field.');
	 	}
 		$query = mysqli_query($dbc,"SELECT * FROM user WHERE name_username = '".$_POST['userName']."'") or die();
	 	//Gives error if user dosen't exist
		$check = mysqli_num_rows($query);
		if ($check == 0) 
		{
 			die('That user does not exist in our database. <a href=registration.php>Click Here to Register</a>');
		}
		 while($info = mysqli_fetch_array( $query )) 	
		{
 			$_POST['passwordLogin'] = stripslashes($_POST['passwordLogin']);
 			$info['password'] = stripslashes($info['password']);
 			//gives error if the password is wrong
 			if (md5($_POST['passwordLogin']) != $info['password']) 
			{
 			die('Incorrect password, please try again.');
 			}
			else 
			{ 	
				$myFile = "test.txt";
				$fh = fopen($myFile, 'a') or die("can't open file");
				$stringData = $_POST['userName']."\n";
				fwrite($fh, $stringData);
				fclose($fh);
				session_start();
				$_SESSION['username'] = $_POST['userName'];
				header ("Location: profile.php");
			}
		}
	}
}

I'm having a problem with the above code where the file writing isn't writing to the file at all.  Any suggestions?  Thanks, in advance, for any help.

Link to comment
https://forums.phpfreaks.com/topic/279495-login-not-appending-to-txt-file/
Share on other sites

Your comment says that your query statement gives error if it fails. But you don't show an error message with simply 'or die()'. Try adding some text to the die and see if it says something. Then add some other echos in the following code to check your progress so you can see how far you are getting and debug your problem.

 

Also - turn on error checking. It will help you debug some other errors. For ex., you have an if statement at the top with a '|' in it, which is not a proper logical connector. ('OR' is usually '||')

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.