8 Replies - 622 Views - Last Post: 05 December 2014 - 07:58 AM Rate Topic: -----

#1 AmehdG   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 03-December 14

Creating an account, validating and checking from file

Posted 03 December 2014 - 11:00 PM

Hey guys!

So Im a but stuck on this php code. Quick recap of my code, this page is required to create a new account, save into a file named 'accounts.txt' once created, read from the text file and sign in with that account. my create account sections works and it writes into a file but when I want to login in it doesn't seem to read from the file. I also believe that my start_session isn't working properly and or is correctly placed where its at.

CREATEACCOUNT.PHP
<?php
//error handling function. remove when you don't need it
function customError($errno, $errstr) {
	echo "<b>Error:</b> [$errno] $errstr<br />";
}
set_error_handler("customError");
?>

<?php
	if(isset($_POST['submit'])) {
			$username = $_POST['username'];
			$password = $_POST['password'];
			$balance = $_POST['balance'];
			
			if(empty($username)) {
					echo "Please enter a username";
				}
			else {
					$username = $username;
				}
			if(empty($password)) {
					echo "Please enter a password";
				}
			else {
					$password = $password;
				}
			//if(empty($balance)) {
			//	echo "Please enter an amount";
			//}
			//else {
			//	$balance = $balance;
			}
			
			$fp = fopen("accounts.txt", "r+");
			$text = $username . "," . $password . "\n";
		
			if(file_exists('accounts.txt')) {
					$contents = file_get_contents($path);
					$contents = explode("\n", $contents);
					$users = array();
					foreach ($contents as $value) {
						$user = explode(',', $value);
						$users[$user[0]] = $user[1];
					}
					if (isset($users[$_POST['name']])) {
						echo "User already exist, please enter a different username";
					}
				}
			if(!empty($username) && !empty($password) && fwrite($fp, $text) && !isset($users[$_Post['name']])) {
				header('Location: login.html');
			}
			fclose($fp);
?>


LOGIN.PHP
<?php
//error handling function. remove when you don't need it
function customError($errno, $errstr) {
	echo "<b>Error:</b> [$errno] $errstr<br />";
}
set_error_handler("customError");
?>
<?php
	session_start();

	$username = $_POST['username'];
	$password = $_POST['password'];
	
	if(isset($_POST['submit'])) {
		$file = explode(PHP_EOL, file_get_contents("accounts.txt"));
		
		if(empty($username)) {
			echo "Enter a username";
		}
		else {
			$username = $username;
		}
		if(empty($password)) {
			echo "Enter a password";
		}
		else {
			$password = $password;
		}
		
		$auth = false;
		foreach($file as $line) {
			list($username, $password) = explode(",", $line);
			if($_POST['username'] == $username && $_POST['password'] == $password) {
				$_SESSION('submit');
				//session_start('submit');
				$auth = true;
				break;
			}
		}
	if($auth) {
		header('Location: index.php');
	}
	else {
		echo "Invalid username and/or password";
		}
	}
?>	



INDEX.PHP
<?php
//error handling function. remove when you don't need it
function customError($errno, $errstr) {
	echo "<b>Error:</b> [$errno] $errstr<br />";
}
set_error_handler("customError");
?>

<?php
	function checkSession() {
		if(session_start() == PHP_SESSION_NONE)
		session_start('submit');
	}	//Start session

	//Check if user is logged and existing session
	if(isset($_SESSION['submit'])) {
		//content for user logged 
		echo "Welcome " .$username. ".";
		//echo "Your account balance is " .$balance. ". <a href='logout.php'>Logout</a>";
	} else {
		//Redirect to login page
		Header("Location: login.html");
	}
?>


Is This A Good Question/Topic? 0
  • +

Replies To: Creating an account, validating and checking from file

#2 CTphpnwb   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3796
  • View blog
  • Posts: 13,742
  • Joined: 08-August 08

Re: Creating an account, validating and checking from file

Posted 04 December 2014 - 04:31 AM

?>
<?php
	session_start();


You can't start a session after anything has been sent to the browser. Even white space like a carriage return. Why would you break out of PHP only t break into it anyway?
Was This Post Helpful? 0
  • +
  • -

#3 AmehdG   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 03-December 14

Re: Creating an account, validating and checking from file

Posted 04 December 2014 - 09:00 AM

so I commented out the break but i get this error

Error: [8] Undefined variable: _SESSION


View PostCTphpnwb, on 04 December 2014 - 04:31 AM, said:

?>
<?php
	session_start();


You can't start a session after anything has been sent to the browser. Even white space like a carriage return. Why would you break out of PHP only t break into it anyway?

Was This Post Helpful? 0
  • +
  • -

#4 ArtificialSoldier   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2041
  • View blog
  • Posts: 6,264
  • Joined: 15-January 14

Re: Creating an account, validating and checking from file

Posted 04 December 2014 - 09:15 AM

Did you comment out the session_start?
Was This Post Helpful? 0
  • +
  • -

#5 AmehdG   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 03-December 14

Re: Creating an account, validating and checking from file

Posted 04 December 2014 - 10:41 AM

View PostArtificialSoldier, on 04 December 2014 - 09:15 AM, said:

Did you comment out the session_start?


I did under login.php
Was This Post Helpful? 0
  • +
  • -

#6 CTphpnwb   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3796
  • View blog
  • Posts: 13,742
  • Joined: 08-August 08

Re: Creating an account, validating and checking from file

Posted 04 December 2014 - 10:56 AM

Why would you do that?

Try to remember that we're not here to hand you solutions. You're going to need to think.
Was This Post Helpful? 0
  • +
  • -

#7 ArtificialSoldier   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2041
  • View blog
  • Posts: 6,264
  • Joined: 15-January 14

Re: Creating an account, validating and checking from file

Posted 04 December 2014 - 11:01 AM

Things like this are the issue:

set_error_handler("customError");
?>

<?php
if(isset($_POST['submit'])) {

There's no reason to break out of PHP if you're just going right back in. Just do this:

set_error_handler("customError");

if(isset($_POST['submit'])) {

Was This Post Helpful? 0
  • +
  • -

#8 AmehdG   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 03-December 14

Re: Creating an account, validating and checking from file

Posted 04 December 2014 - 03:36 PM

View PostCTphpnwb, on 04 December 2014 - 10:56 AM, said:

Why would you do that?

Try to remember that we're not here to hand you solutions. You're going to need to think.


In no way trying to get solutions just some guidance and different ideas
But thanks I'm on to something now
Was This Post Helpful? 0
  • +
  • -

#9 ArtificialSoldier   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2041
  • View blog
  • Posts: 6,264
  • Joined: 15-January 14

Re: Creating an account, validating and checking from file

Posted 05 December 2014 - 07:58 AM

What code are you using now?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1