session_start(); # start up the session
$host="localhost"; // Host name 
$db_username="root"; // Mysql username 
$db_password=""; // Mysql password 
$db_name="shop"; // Database name 
$tbl_name="users"; // Table name 

// Connect to server and select databse.
mysql_connect("$host", "$db_username", "$db_password")or die("cannot connect"); 
mysql_select_db("$db_name")or die("cannot select DB");

// username and password sent from form 
$password = md5(trim($psswd));

// To protect MySQL injection (more detail about MySQL injection)
$username = stripslashes($username);
$password = stripslashes($password);
$username = mysql_real_escape_string($username);
$password = mysql_real_escape_string($password);
$sql="SELECT * FROM users WHERE username='$username' and password='$password'";

// Mysql_num_row is counting table row

// If result matched $username and $password, table row must be 1 row

// Register $myusername, $mypassword and redirect to file "login_success.php"
$_SESSION['logged_in'] = true;


ok,i have this login.php script but i want to create some parts of my web to be availabale only for logged users i've tried with $_SESSION[logged_in]=false; but it doesnt work

To ensure only logged-in users enter a particular page, add this code to the top of the page.

if (!isset($_SESSION['logged_in']) || ($_SESSION['logged_in'] == false)){

You say you tried this and it didn't work. Could you be a bit more specific please?
Did you get any error messages?
What happened that wasn't supposed to?
What didn't happen that was supposed to?

thanks a lot a tried this but i forgot to put session start() on the top of the code. Now its work.

i'm new in PHP but this is my last question how to display the name of the logged user i tried with this one but it shows me an error that username is undefined index

<?php echo $_SESSION['username']?>

And should i connect to my database and table and then ,to put this code on the place where i like .

In order for you to be able to access data at $_SESSION['username'], you have to have assigned data to there previously. It worked for $_SESSION['loggedin'] because you assigned a value to it on line 33. To store the username, you can add this after line 33:

$_SESSION['username'] = $username;

As EvolutionFallen said but also don't forget you need to put session_start() at the top of ALL pages that set, change or display $_SESSION variables.

echo $_SESSION['username'];

You don't need to connect to the database when you display session variables. The username probably comes from a database so you may need connect to retrieve it before you can set it as a session variable.