Checking for sessions

then is it not possible to redirect my page to “products.php” page
in the above code…

yes, because you have built in an error by echoing the message. Even the thinking is wrong …

Lets ASSUME PHP could work that way. The usser would never see your message anyway… as the page would be redirected immediately to products.php. So again this wouldn’t be something you want to do, even if PHP allowed it.

i am not bothered about the $message variable but first it should redirect my page to
“products.php”.

Just remove echo $message; and it will redirect fine then. The thing to keep in mind is that the variable will be lost in the redirect. So you wont be able to echo it in the next page either.

Also note what I said about your logic.

Additionally … you leave yourself wide open for $message being “user:”.timestamp ( again i dont see why you would want this).

if i remove echo $message every time it is re directing to header(“Location:login3.php”);
why it is used for

Because that’s what your code is set up to do when $rows is 0


if ($rows > 0) { 
    ...
} else { 
    //unsuccessful login 
    header("Location:login3.php");
    exit; 
}

but i have 2 rows in my login table with 4 fields…

Thank you, Paul_w. That’s what I mean by checking your logic. Your PHP is working, but the way you are setting up your if/then statements may not function the way you think. I recommend you make a flowchart ( on paper) and follow though, pretending you were the computer, and see where each possible input leads you.

it also possible you may not be getting the input from your DB that you think you are.

So, you need to test now if the database is returning what you expect to receive.
It can be easier to test if you separate the query in to $sql and $result, so that you can var_dump $sql to check that things are as you expect them to be.


$sql = "SELECT * from ...";
var_dump($sql);
$result = mysql_query($sql); 

first when the session has started he need to go to the “products.php” page
using the session name(here using time()).here username is the output of time()
that should be stored in the database.
tell me how to do this one…

instead of if ($rows > 0) is it possible to check whether the user is already present or not

Yes it is possible, just by adjusting your request to the database.

not by using time() right…

Which presence are you wishing to check? Presence within the database or presence as a sessioned user?

as a sessioned user

Then you would check if $_SESSION[‘username’] is equal to the supplied username.

that i have done alreagy in my below if statement right
if ($rows > 0)
{
}

whether the below code is correct


<?php 
session_start();
mysql_connect("localhost","root","") or die("mysql_error()");
mysql_select_db("shopping") or die("mysql_error()");
$result = mysql_query("SELECT * from login where username='" . $_POST['username'] . "' AND password='" . $_POST['password'] . "'"); 
$rows=mysql_num_rows($result);
if(!isset($_SESSION['username']))
{
 $_SESSION['username'] = $_POST['username'];
} 
else 
{
  $_SESSION['username'] = time();
}


now tell me if the username is matching with the username in database
then how to redirect to “products.php”

That’s where you want to learn about performing SQL queries.
It should only take a modification to your already existing SQL query to achieve that.

k below i sthe way the code has to flow


<?php 
session_start();
mysql_connect("localhost","root","") or die("mysql_error()");
mysql_select_db("shopping") or die("mysql_error()");
$result = mysql_query("SELECT * from login where username='" . $_POST['username'] . "' AND password='" . $_POST['password'] . "'"); 
$rows=mysql_num_rows($result);
if(isset($_SESSION['username']))
{
//if the username is matching in the database go to page "products.php" 

} 
else 
{
//also go to page "products.php" by creating new user in the database
 }

for this how the above code should be modified…