Log in

View Full Version : parse error in php



ravi951
09-21-2011, 05:18 AM
hi all,
i have written a code using php for sessions.it will checks whether the
user is new or already registered...
i am getting the error as
Parse error: syntax error, unexpected T_ELSE in C:\xampp\htdocs\shopping1\task.php on line 16
kindly tell me what went wrong.
below is the code......


<?php
define("LIMIT", 10);
session_start();
//connect to database
$db = mysql_connect("localhost","root","") or die(mysql_error());
mysql_select_db("shopping", $db) or die(mysql_error());
$islogged = FALSE;
if(!isset($_SESSION["last_activity"]) || time() - $_SESSION["last_activity"] > LIMIT )
{
session_destroy();
header("Location:logout.php");
}
$_SESSION["last_activity"] = time();
$islogged = TRUE;

else
{
if(isset($_POST["username"]) && isset($_POST["password"]))
{
$result = mysql_query("SELECT * FROM login WHERE `username` = '$_POST["username"]' AND `password` = '$_POST["password"]'");
if(!$result) die( mysql_error());
if(mysql_num_rows($result))
{
$_SESSION["last_activity"] = time();
header("Location:products.php");
$islogged = TRUE;
}
else
{
$error = "username and password do not match";
}
}
}
?>

<?php if(!$islogged): ?>
<form action="<?php $_SERVER['HTTP_REQUEST']?>" method="POST">
<?php if( isset($error) ): ?>
<p><?php echo $error;?></p>
<?php endif; ?>
Username:<input type="text" name="username" value="<?php isset($_POST['username']) ? $_POST['username'] : ''?>"
</br>
Password:<input type="password" name="password" value="<?php isset($_POST['password']) ? $_POST['password'] : ''?>"
</br>
<input type="submit" name="login" value="log in">
</form>
<?php endif; ?>

djr33
09-21-2011, 05:50 AM
You can't have anything between your IF and ELSE statements.

if (condition) { code_to_execute } else { code_to_execute }


What you have is:

if (condition) { code_to_execute }
PROBLEM CODE
else { code_to_execute }


Unless that is just a typo, you will need to explain what you want to accomplish because that code does not make any sense right now.

ravi951
09-21-2011, 05:53 AM
i have modified like below


if(!isset($_SESSION["last_activity"]) || time() - $_SESSION["last_activity"] > LIMIT )
{
session_destroy();
header("Location:logout.php");

$_SESSION["last_activity"] = time();
$islogged = TRUE;
}
else
{
if(isset($_POST["username"]) && isset($_POST["password"]))

but it is displaying the error as
Parse error: syntax error, unexpected '"', expecting T_STRING or T_VARIABLE or T_NUM_STRING in C:\xampp\htdocs\shopping1\task.php on line 19

traq
09-21-2011, 07:00 AM
but it is displaying the error as
Parse error: syntax error, unexpected '"', expecting T_STRING or T_VARIABLE or T_NUM_STRING in C:\xampp\htdocs\shopping1\task.php on line 19


read the error message. go to line 19 and start looking for a double-quote that is out of place. If you don't find one, look for others typos on or a bit before that line.

If you want to write a PHP script, you're going to _have_ to become familiar with basic syntax. PHP error messages are actually very straightforward, and make debugging very easy. they're *almost* instructions.

in addition, this still doesn't make sense:
if(!isset($_SESSION["last_activity"]) || time() - $_SESSION["last_activity"] > LIMIT )
{
session_destroy();
header("Location:logout.php"); // you can't _leave_ this script...

$_SESSION["last_activity"] = time(); // and _then_ do something.
$islogged = TRUE;
// these two lines need to be somewhere else.
// personally, I think they're supposed to be part of the else{} block,
// but you'd have to answer that one.
}

ravi951
09-21-2011, 08:58 AM
how to check whether there is session in the code if there is a session
then using that session (means using time) he has to enter
or else
if there is no session then create the new session and get in to code....
whether the above code will work

traq
09-21-2011, 07:01 PM
in the excerpt above,
if(!isset($_SESSION["last_activity"]) || time() - $_SESSION["last_activity"] > LIMIT )
{
session_destroy();
header("Location:logout.php");
// if last_activity is not set (or expired), then you're redirecting them to logout.php.
// typically, the rest of _this_ script will not finish executing
// (though it may - results will be unpredictable).
// this means that the two following lines _might_ happen, but probably _won't_.

$_SESSION["last_activity"] = time();
$islogged = TRUE;
// so, what are you trying to do?
// I'm not sure why you would want to log the user out,
// and then immediately give them a valid "last_activity"
// (which would seem to imply you're logging them back in)...?
}
if there is a session
then using that session (means using time) he has to enter
or else
if there is no session then create the new session and get in to code....
whether the above code will workSince I'm not clear on what you're trying to do, I'm not sure if your code "will work."

It seems pointless to check the "last_activity" if all you do in the end is give it a valid value anyway.

ravi951
09-22-2011, 03:41 AM
k let me explain clearly.
first i want to check whether session exists or not.
if not exists then create a session by giving a random no to it..
thats it how to do that.......

traq
09-22-2011, 04:38 AM
well, here's the deal:
session_start();
// session_start does this automatically.
// if a valid session id is provided, it uses it.
// if not, it creates one.
if you want sessions that will "expire" after a certain amount of time, your logic would be a little different:
session_start();
if(empty($_SESSION["last_activity"]) || time() - $_SESSION["last_activity"] > LIMIT){
// session is too old. logout.
}else{
// session is fine. update the "last_activity" to the current time.
}
// putting those actions under the same condition
// (as you had above) makes little sense.
// it defeats the purpose of checking,
// as the outcome is always the same.

ravi951
09-22-2011, 04:44 AM
hi
i am making it simple
below is the code i have written to check the session exists or not...


<?php
session_start();
mysql_connect("localhost","root","") or die("mysql_error()");
mysql_select_db("shopping") or die("mysql_error()");
if(!isset($_SESSION['username']))
{
print "Your sessionID: ".time();
}
else
{
print "Session does not exist";
}
session_destroy();
?>

tell me how to mdify the above code that stores session id genetate in database.
i have login field in database with 4 fields namely id,username,password and time.....

traq
09-22-2011, 02:17 PM
do you already have a login script? if so, please post it.

ravi951
09-23-2011, 04:43 AM
below is my modified script. but it is not redirecting to the
page "products.php"
what went wrong here....


<?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']))
{
$messages = "UserId : ".$_SESSION['username'];
unset($_SESSION['username']);
}
else
{
$_SESSION['username'] = time();
}
if ($rows > 0)
{
session_register('username');
$_SESSION['username'] = $_POST['username'];
echo $messages;
header("Location:products.php");
exit;
}
else
{
//unsuccessful login
header("Location:login3.php");
exit;
}
?>

traq
09-23-2011, 05:23 AM
is it redirecting to login3.php ? is it giving you an error message? please be as specific as possible about what's going on.

in addition, it is always best to use absolute URLs when you use the header() (http://us3.php.net/manual/en/function.header.php) function:
header("Location: http://www.example.com/page.php");
// is much more reliable than
header("Location: page.php");
// while this works sometimes,
// according to the HTTP/1.1 spec, it technically shouldn't work at all.

ravi951
09-23-2011, 05:28 AM
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....