ravi951
September 21, 2011, 5:13am
1
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\ ask.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; ?>
That error means you’re using and ELSE where there can’t be one. Take a look at the first ELSE (and the lines above it) in that piece of code you posted. Do you see what the problem is?
ravi951
September 21, 2011, 5:42am
3
i think else should be used after the closing brace.is that so?
ravi951
September 21, 2011, 5:48am
5
i have done like that also 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\ ask.php on line 19
ravi951:
i have done like that also 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\ ask.php on line 19
Post the new, modified script.
ravi951
September 21, 2011, 5:56am
7
k below is my modified script…
<?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; ?>
This is line 19:
$result = mysql_query("SELECT * FROM login WHERE `username` = '$_POST["username"]' AND `password` = '$_POST["password"]'");
The problem is the double quotes around username and password. Try writing it like this:
$result = mysql_query("SELECT * FROM login WHERE `username` = '" . $_POST["username"] . "' AND `password` = '" . $_POST["password"] . "'");
And please, when you post php code, put php tags around it instead of code tags: [noparse]
[/noparse]
ravi951
September 21, 2011, 6:09am
9
i have replaced like u said but this time it is displaying the mesage as
Internet Explorer cannot display the webpage
below is my modified 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; ?>
ravi951
September 21, 2011, 6:18am
10
in my output both the username and password and login button are in a single line
tell me how to do it so that we get username in one line and password in next line
and login button in next line…
below is the code…
<?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; ?>
if(!isset($_SESSION["last_activity"]) || time() - $_SESSION["last_activity"] > LIMIT )
{
session_destroy();
header("Location:logout.php");
[B][COLOR="#FF0000"]$_SESSION["last_activity"] = time();
$islogged = TRUE;[/COLOR][/B]
}
Are you sure those two lines in red should be there? After the redirect you should put an exit();
And my guess is you want those two lines somewhere else in your code.
if(!isset($_SESSION["last_activity"]) || time() - $_SESSION["last_activity"] > LIMIT )
{
session_destroy();
header("Location:logout.php");
[B][COLOR="#008000"]exit();[/COLOR][/B]
}
ravi951
September 21, 2011, 6:28am
12
yes u are right if i put exit() like u said then it is unable to display the page…
ravi951
September 21, 2011, 6:36am
14
the modification i have done like u said is not executing.
tell me how to make a time field in my login table so that it stores when the users are logged in…
below is the modified 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_unset();
header("Location:logout.php");
exit();
/* $_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; ?>
What does that mean? What do you see? The login form? The logout page? An error?
tell me how to make a time field in my login table so that it stores when the users are logged in…
ALTER TABLE ADD COLUMN
ravi951
September 21, 2011, 8:54am
16
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