PHP Parse Error...
Results 1 to 7 of 7

Thread: PHP Parse Error...

  1. #1
    Banned
    Join Date
    Oct 2001
    Posts
    1,463

    PHP Parse Error...

    Ok, IM trying to write the code to my PHP Program that takes a variable from the browser (book.php?page=1)... I think Im going in the right direction but when I try to execute the following code it gives me a parse eroor:
    Code:
    Parse error: parse error, unexpected '\"', expecting T_STRING or T_VARIABLE or T_NUM_STRING in D:\Apache\htdocs\book.php on line 2
    Here is my PHP code...
    PHP Code:
    <?php
    header
    ("location: $_GET["page"]");
    if (
    page 1) {
        print(
    "www.antionline.com");
    }
    ?>
    Any suggestionPS: What im trying to get with this code is that it will take the variable(?page=1) read it into the program and redirect the user to a specific output depending on that variable...

  2. #2
    Banned
    Join Date
    Jun 2002
    Posts
    458
    Here is somebody with your problem and some solutions.
    <?php
    print "Your first name is $HTTP_POST_VARS['FirstName'].
    \n";
    print "Your last name is $HTTP_POST_VARS['LastName'].
    \n";
    print "Your E-mail address is $HTTP_POST_VARS['Email'].
    \n";
    print "This is what you had to say:
    \n $HTTP_POST_VARS['Comments']
    \n";
    ?>

    just change the double quotes to single quotes for all $HTTP_POST_VARS[''] variables.
    Hope dis helps.

    EDIT:
    "You can still force the old behaviour by setting register_globals to 'On' in your php.ini file."
    Sorry I didn't post the link last time, here it is, http://www.webmasterworld.com/forum13/1223.htm

  3. #3
    Banned
    Join Date
    Oct 2001
    Posts
    1,463
    Ok, Thanks for that help but now thee is another problem..... Ive assigned a variable to page ($page) but whenever I try to access the page to test it it says
    Code:
    Forbidden
    You don't have permission to access /$_GET["page"] on this server.
    
    
    --------------------------------------------------------------------------------
    
    Apache/1.3.26 Server at localhost Port 1337
    My code is as follows:
    PHP Code:
    <?php
    $page 
    header('location: $_GET["page"]');
    if (
    $page 1)
        print 
    "www.antionline.com";
    ?>
    Is there an easier way to see what variabe is out into the script then act on it?

  4. #4
    Banned
    Join Date
    Jul 2002
    Posts
    11
    asddf

  5. #5
    Hi mom!
    Join Date
    Aug 2001
    Posts
    1,103
    You're trying to mix two functions together, acid.

    The header('location: page') funcition redirects the browser to another page.
    in the array $_GET are the variables you included in the url.

    So, the correct version of that script would be:

    PHP Code:
    <?php
    $page 
    $_GET["page"];   // this reads the variable "page" in the array $_GET
    if ($page == 1) {
       
    // do the page one stuff here
    }
    ?>
    Instead of the if-statement, you might want to use the switch/case statement

    PHP Code:
    <?php
    $page 
    $_GET["page"];   // this reads the variable "page" in the array $_GET
    switch ($page) {
       case 
    1:
          
    // do the page one stuff here
          
    break;
       case 
    2:
          
    // page two stuff
          
    break;
       case 
    3:
          
    // page three stuff
          
    break;
        case default:
          
    // do stuff you need to do when an unexpected value is found
    }
    ?>
    Oh, and by the way: $HTTP_POST_VARS and $HTTP_GET_VARS are deprecated. Use $_POST and $_GET instead.

    You can find a bunch of information on the php.net website. Here's a link to their manual: http://www.php.net/manual/
    I wish to express my gratitude to the people of Italy. Thank you for inventing pizza.

  6. #6
    Banned
    Join Date
    Oct 2001
    Posts
    1,463
    Guus, How can I get the content to be redirected.... say the content I want for page 1 is perl.pdf... How can I redirect to that

  7. #7
    Hi mom!
    Join Date
    Aug 2001
    Posts
    1,103
    Try this:

    PHP Code:
    <?php
    $page 
    $_GET["page"];   // this reads the variable "page" in the array $_GET
    switch ($page) {
       case 
    1:
          
    header"Content-Type: text/plain" );
          
    readfile"firstfile.txt" );
          break;
       case 
    2:
          
    header"Content-Type: text/html" );
          
    readfile"secondfile.html" );
          break;
       case 
    3:
          
    header"Content-Type: audio/x-wav" );
          
    readfile"third.wav" );
          break;
       case 
    4:
          
    header"Content-Type: application/pdf" );
          
    readfile"perl.pdf" );
          break;
        case default:
          
    header"Content-Type: text/html" );
          
    readfile"home.html" );
    }
    ?>
    I never actually used this myself, but I'm pretty sure it'll work (save some syntax errors).

    Hmmz, I was reading up a bit on php security, sql injection, and other stuff I could find, and I came across this tutorial by chsH. If I understand him correctly, you could filter out all unwanted characters by using the preg_replace() call.

    To make sure the variables you GET from your url only include numbers, you could change the line that now reads:

    PHP Code:
    $page $_GET["page"]; 
    in:

    PHP Code:
    $page preg_replace("/[^0-9]/","",$_GET["page"]); 
    You could also add some length-checking, makeing sure your variables don't include more than a couple of numbers. Read chsh's thread for more information.
    I wish to express my gratitude to the people of Italy. Thank you for inventing pizza.

Posting Permissions

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

 Security News

     Patches

       Security Trends

         How-To

           Buying Guides