SitePoint Sponsor

User Tag List

Results 1 to 16 of 16
  1. #1
    SitePoint Addict
    Join Date
    Jul 2011
    Location
    Hyderabad,India.
    Posts
    327
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    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:\xampp\htdocs\shopping1\task.php on line 16
    kindly tell me what went wrong.
    below is the code......
    PHP 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; ?>
    Last edited by guido2004; Sep 20, 2011 at 22:38. Reason: changed tags from code to php

  2. #2
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,400
    Mentioned
    147 Post(s)
    Tagged
    4 Thread(s)
    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?

  3. #3
    SitePoint Addict
    Join Date
    Jul 2011
    Location
    Hyderabad,India.
    Posts
    327
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    i think else should be used after the closing brace.is that so?

  4. #4
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,400
    Mentioned
    147 Post(s)
    Tagged
    4 Thread(s)
    exactly

  5. #5
    SitePoint Addict
    Join Date
    Jul 2011
    Location
    Hyderabad,India.
    Posts
    327
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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\task.php on line 19

  6. #6
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,400
    Mentioned
    147 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by ravi951 View Post
    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\task.php on line 19
    Post the new, modified script.

  7. #7
    SitePoint Addict
    Join Date
    Jul 2011
    Location
    Hyderabad,India.
    Posts
    327
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    k below is my modified script..
    PHP 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; ?>
    Last edited by guido2004; Sep 20, 2011 at 22:58. Reason: changed tags from code to php

  8. #8
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,400
    Mentioned
    147 Post(s)
    Tagged
    4 Thread(s)
    This is line 19:
    PHP Code:
    $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:
    PHP Code:
    $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: [php] [/php]

  9. #9
    SitePoint Addict
    Join Date
    Jul 2011
    Location
    Hyderabad,India.
    Posts
    327
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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 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; ?>

  10. #10
    SitePoint Addict
    Join Date
    Jul 2011
    Location
    Hyderabad,India.
    Posts
    327
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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 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; ?>

  11. #11
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,400
    Mentioned
    147 Post(s)
    Tagged
    4 Thread(s)
    Code:
    if(!isset($_SESSION["last_activity"]) || time() - $_SESSION["last_activity"] > LIMIT ) 
    {
        session_destroy();
        header("Location:logout.php");
        $_SESSION["last_activity"] = time();
        $islogged = TRUE;
    }
    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.
    Code:
    if(!isset($_SESSION["last_activity"]) || time() - $_SESSION["last_activity"] > LIMIT ) 
    {
        session_destroy();
        header("Location:logout.php");
        exit();
    }

  12. #12
    SitePoint Addict
    Join Date
    Jul 2011
    Location
    Hyderabad,India.
    Posts
    327
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    yes u are right if i put exit() like u said then it is unable to display the page......

  13. #13
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,400
    Mentioned
    147 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by ravi951 View Post
    yes u are right if i put exit() like u said then it is unable to display the page......
    Unable?

  14. #14
    SitePoint Addict
    Join Date
    Jul 2011
    Location
    Hyderabad,India.
    Posts
    327
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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 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; ?>

  15. #15
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,400
    Mentioned
    147 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by ravi951 View Post
    the modification i have done like u said is not executing.
    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

  16. #16
    SitePoint Addict
    Join Date
    Jul 2011
    Location
    Hyderabad,India.
    Posts
    327
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    reply

    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


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •