/    Sign up×
Community /Pin to ProfileBookmark

parse error in php

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:xampphtdocsshopping1task.php on line 16
kindly tell me what went wrong.
below is the code……

[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; ?>
[/CODE]

to post a comment
PHP

3 Comments(s)

Copy linkTweet thisAlerts:
@fideltfgSep 21.2011 — 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:xampphtdocsshopping1task.php on line 16

kindly tell me what went wrong.

below is the code......
[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; ?>
[/CODE]
[/QUOTE]


that error can mean a few things, but try looking for missed semi-colons or } check the else on line 16

Also you do not need to endif, an if statement is like this
[code=php]if(condition){
$code = $xyz;
}[/code]


and an if else is like this
[code=php]if(condition){
$code = $xyz;
}else{
$code = $abc;
}
[/code]


and lastly an else if is like this
[code=php]if(condition){
$code = $xyz;
}elseif(condition two){
$code = $abc;
}[/code]
Copy linkTweet thisAlerts:
@ravi951authorSep 21.2011 — i have done like that also.below is my modified script..

but this time it is displaying the error as [B]Parse error: syntax error, unexpected '"', expecting T_STRING or T_VARIABLE or T_NUM_STRING in C:xampphtdocsshopping1task.php on line 19

[/B]


below is my modified script.
[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; ?>
[/CODE]
Copy linkTweet thisAlerts:
@fideltfgSep 21.2011 — ok, your problem is with this line(20)

[code=php]$result = mysql_query("SELECT * FROM login WHERE username = '$_POST["username"]' AND password = '$_POST["password"]'");[/code]

try it like this

[code=php]$result = mysql_query("SELECT * FROM login WHERE username = '".$_POST['username']." AND password = '".$_POST['password']."'");[/code]

You can not ( or should not) I can never remember which) put calls to array elements in to a string like you can a simple variable it must be add to the string using the . also you do not require the ` on column names and array elements are best referenced with ' as apposed to ", it will just help later when you have huge queries and hundreds of quotes
×

Success!

Help @ravi951 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.19,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...