tags:

views:

227

answers:

3

I'm learning PHP and main my concern is adding activity to my website, but I don't know SQL. Is there a way to do this without SQL?

A: 

You can probably use flat files - only concern might be parallel adding of new accounts. But if you want to dig deeper in website development, SQL is definitely good to dive into.

Sascha
A: 

For a quick & dirty solution, you could store user credentials in an array, e.g:

$creds = array( 
               array( 'username' => 'john123',
                      'password' => 'hello'),
               array( 'username' => 'lucyliu',
                      'password' => 'dieblackmamba')
                   //more sub arrays like above here
         );

You can then match the user's input, e.g.:

$username = $_POST['username'];
$password = $_POST['password'];

$match = false;
for($i=0;$i<count($creds);$i++) {
    if(strcmp($username,$creds[$i]['username']) == 0) {
        if(strcmp($password,$creds[$i]['password']) == 0) {               
            // start session and set something to indicate that user is logged in
            session_start();
            $_SESSION['authenticated'] = true;
            $_SESSION['username'] = $creds[$i]['username'];
            $match = true;    
        }
    }
}

if($match) echo 'Login successful: welcome ' . $creds[$i]['username'];
    else echo 'Invalid credentials';

EDIT: On subsequent page call, you can check if the user is still logged in by reading the $_SESSION

session_start();
if($_SESSION['authenticated'] === true) {
     echo 'User ' . $_SESSION['username'] . ' is logged in.';
}

To log out the user, you can execute this code:

 $_SESSION = array();
 // If it's desired to kill the session, also delete the session cookie.
 // Note: This will destroy the session, and not just the session data!
 if (isset($_COOKIE[session_name()])) {
  setcookie(session_name(), '', time()-42000, '/');
 }
 // Finally, destroy the session.
 session_destroy();

It would be helpful for you to read about php sessions and arrays, and if I were you I would bite the bullet and try to get into SQL.

karim79
A: 

Check how session handling works http://us.php.net/session, this way you can save your user object.

Just saving your session works in a one server environment, in a multi server environment you would need to save your session to a common place such as a database, but for simpler pages and learning just saving to a session works

Iman