Page 1 of 2 12 LastLast
Results 1 to 10 of 13

Thread: parse error in php

  1. #1
    Join Date
    Jul 2011
    Location
    hyderabad,India
    Posts
    58
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default 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......
    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; ?>

  2. #2
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    You can't have anything between your IF and ELSE statements.

    if (condition) { code_to_execute } else { code_to_execute }


    What you have is:
    Code:
    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.
    Daniel - Freelance Web Design | <?php?> | <html>| espa˝ol | Deutsch | italiano | portuguŕs | catalÓ | un peu de franšais | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  3. #3
    Join Date
    Jul 2011
    Location
    hyderabad,India
    Posts
    58
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default reply

    i have modified like below
    Code:
    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

  4. #4
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,643
    Thanks
    63
    Thanked 516 Times in 502 Posts
    Blog Entries
    5

    Default

    Quote Originally Posted by ravi951 View Post
    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:
    PHP Code:
    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.


  5. #5
    Join Date
    Jul 2011
    Location
    hyderabad,India
    Posts
    58
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default check whether there is session

    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

  6. #6
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,643
    Thanks
    63
    Thanked 516 Times in 502 Posts
    Blog Entries
    5

    Default

    in the excerpt above,
    PHP Code:
    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)...?

    Quote Originally Posted by ravi951
    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
    Since 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.

  7. #7
    Join Date
    Jul 2011
    Location
    hyderabad,India
    Posts
    58
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default reply

    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.......

  8. #8
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,643
    Thanks
    63
    Thanked 516 Times in 502 Posts
    Blog Entries
    5

    Default

    well, here's the deal:
    PHP Code:
    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:
    PHP Code:
    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. 

  9. #9
    Join Date
    Jul 2011
    Location
    hyderabad,India
    Posts
    58
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default reply

    hi
    i am making it simple
    below is the code i have written to check the session exists or not...
    PHP Code:
    <?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.....

  10. #10
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,643
    Thanks
    63
    Thanked 516 Times in 502 Posts
    Blog Entries
    5

    Default

    do you already have a login script? if so, please post it.

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
  •