please help me fix my php tuts web

For the discussion of Perl, Python, Ruby, and PHP and other interpreted languages.

please help me fix my php tuts web

Post by chard5230 on Mon Mar 28, 2011 11:31 am
([msg=55640]see please help me fix my php tuts web[/msg])

im trying to fix my code for a week and i haven't find a solution for my problem

does anyone know how to fix this..!
im currently a student wanted to learn PHP on my own
so i do some research

and this is my problem i cant fix this
Parse error: syntax error, unexpected '}' in C:\xampp\htdocs\forum\functions.php on line 84

the line 84 is the 2nd curly braces:
Code: Select all
header("Location: " . $config_basedir . "/login.php?error=1");
}
}
else {
require("header.php");



this is the whole source code for my website

Code: Select all
<?php

function pf_script_with_get($script) {
$page = $script;
$page = $page . "?";

foreach($_GET as $key => $val) {
    $page = $page . $key . "=" . $val . "&";
}
return substr($page, 0, strlen($page)-1);
}

?>

<?php

session_start();
require("config.php");
require("functions.php");

$db = mysql_connect($dbhost, $dbuser, $dbpassword);
mysql_select_db($dbdatabase, $db);
$db = mysql_connect($dbhost, $dbuser, $dbpassword);
mysql_select_db($dbdatabase, $db);

if($_POST['submit']) {
$sql = "SELECT * FROM users WHERE username = '"
. $_POST['username'] . "' AND password = '"
. $_POST['password'] . "';";
$result = mysql_query($sql);
$numrows = mysql_num_rows($result);
$result = mysql_query($sql);
$numrows = mysql_num_rows($result);

if($numrows == 1) {
$row = mysql_fetch_assoc($result);
if($row['active'] == 1) {
session_register("USERNAME");
session_register("USERID");
$_SESSION['USERNAME'] = $row['username'];
$_SESSION['USERID'] = $row['id'];

switch($_GET['ref']) {
case "newpost":
if(isset($_GET['id']) == FALSE) {
header("Location: " . $config_basedir .
"/newtopic.php");
}
else {
header("Location: " . $config_basedir .
"/newtopic.php?id=" . $_GET['id']);
}
break;

case "reply":
if(isset($_GET['id']) == FALSE) {
header("Location: " . $config_basedir .
"/newtopic.php");
}
else {
header("Location: " . $config_basedir .
"/newtopic.php?id=" . $_GET['id']);
}
break;
default:
header("Location: " . $config_basedir);
break;
}
}
else {
require("header.php");
echo "This account is not verified yet. You were emailed a link
to verify the account. Please click on the link in the email to
continue.";
}
echo "This account is not verified yet. You were emailed a link
to verify the account. Please click on the link in the email to
continue.";
}
}
else {
header("Location: " . $config_basedir . "/login.php?error=1");
}
}
else {
require("header.php");

if($_GET['error']) {
echo "Incorrect login, please try again!";
}

?>
<form action="<?php echo pf_script_with_get($SCRIPT_NAME); ?>"
method="post">
Don't have an account? Go and <a href="register.php">Register</a>!
<?php
}
require("footer.php");
?>


i forgot to say... im new here... i just got registered recently!
chard5230
New User
New User
 
Posts: 2
Joined: Mon Mar 28, 2011 11:21 am
Blog: View Blog (0)


Re: please help me fix my php tuts web

Post by Gatito on Mon Mar 28, 2011 12:06 pm
([msg=55642]see Re: please help me fix my php tuts web[/msg])

chard5230 wrote:i forgot to say... im new here... i just got registered recently!

Welcome to HTS.
I want to address several things:

First
You should ALWAYS comment your code !

Second
chard5230 wrote:Parse error: syntax error, unexpected '}' in C:\xampp\htdocs\forum\functions.php on line 84

I checked the code and if i counted right here is my evaluation:

Read comments!!!
Code: Select all
//All brackets are properly nested until here

else {                // opening bracket #1
header("Location: " . $config_basedir . "/login.php?error=1");
}                    // Closing bracket #1
}                   // Closing bracket #2 ( This one misses the opening bracket )

else {             // Opening bracket #3 ( This one misses the closing bracket, see below )
require("header.php");
if($_GET['error']) {           // Opening bracket #4
echo "Incorrect login, please try again!";
}                                     // Closing bracket #4

                                      // A closing bracket for #3 isn't present
?>


Try repositioning bracket #2 so that it fits with bracket #3. Try putting it after the require statement. Or delete both of them.

Third
The 'else' at require(header.php) doesn't have a corresponding 'if', so you'll get an error about that. Now I don't know why you would make such a complicated logic nesting ending with an 'else' that loads what seems to be the header, when you also require the header at an other part in your code, there where the user validation fails. What I make of this is that you think that even if the validation fails or not the header will be loaded anyway. Require() doesn't work that way, it literary copies the content of the header.php. What you did is practically loaded header.php twice.
Anyway that 'else' is trouble, get rid of it.

Fourth
I also found this at the end of the code.
Code: Select all
<?php
}       <---- ?
require("footer.php");
?>

That random bracket doesn't do much there, delete it.

Fifth
There is what seems to be duplicated code
Code: Select all
$db = mysql_connect($dbhost, $dbuser, $dbpassword);
mysql_select_db($dbdatabase, $db);
$db = mysql_connect($dbhost, $dbuser, $dbpassword);
mysql_select_db($dbdatabase, $db);

Code: Select all
$result = mysql_query($sql);
$numrows = mysql_num_rows($result);
$result = mysql_query($sql);
$numrows = mysql_num_rows($result);   

Gone wild with copy/paste? Delete the duplicate.

Do those changes and give me feedback.
User avatar
Gatito
Poster
Poster
 
Posts: 113
Joined: Tue Mar 08, 2011 12:55 pm
Blog: View Blog (0)


Re: please help me fix my php tuts web

Post by chard5230 on Mon Mar 28, 2011 10:35 pm
([msg=55666]see Re: please help me fix my php tuts web[/msg])

heres the book, from jono bacon the creator of ubuntu

i just copy the code and understand how it works.. but it seems even the book was wrong?

scroll a little bit, find the page and go to page 143 to 146
you will find the code for functions.php.

Code: Select all
http://www.slideshare.net/guest8aafdd/practical-php-and-mysql


i try to do what you've said, i do all you suggestions
but more errors display when i delete or reposition those brackets.
chard5230
New User
New User
 
Posts: 2
Joined: Mon Mar 28, 2011 11:21 am
Blog: View Blog (0)


Re: please help me fix my php tuts web

Post by kujinR on Tue Mar 29, 2011 1:00 am
([msg=55668]see Re: please help me fix my php tuts web[/msg])

Actually, you don't have to move anything. You forgot one curly brace. Can't remember which one, though.

This is why proper indentation is very important.
Code: Select all
<?php

   function pf_script_with_get($script) {
      $page = $script;
      $page = $page . "?";
   
   foreach($_GET as $key => $val) {
      $page = $page . $key . "=" . $val . "&";
   }
   return substr($page, 0, strlen($page)-1);
   }

?>

<?php

   session_start();
   require("config.php");
   require("functions.php");
   
   $db = mysql_connect($dbhost, $dbuser, $dbpassword);
   mysql_select_db($dbdatabase, $db);
   $db = mysql_connect($dbhost, $dbuser, $dbpassword);
   mysql_select_db($dbdatabase, $db);
   
   if($_POST['submit']) {
      $sql = "SELECT * FROM users WHERE username = '"
      . $_POST['username'] . "' AND password = '"
      . $_POST['password'] . "';";
      
      $result = mysql_query($sql);
      $numrows = mysql_num_rows($result);
   
      if($numrows == 1) {
      $row = mysql_fetch_assoc($result);
      
         if($row['active'] == 1) {
            session_register("USERNAME");
            session_register("USERID");
            $_SESSION['USERNAME'] = $row['username'];
            $_SESSION['USERID'] = $row['id'];
         
            switch($_GET['ref']) {
               case "newpost":
                  if(isset($_GET['id']) == FALSE) {
                     header("Location: " . $config_basedir .   "/newtopic.php");
                  }
                  else {
                     header("Location: " . $config_basedir .   "/newtopic.php?id=" . $_GET['id']);
                  }
                  break;
               
               case "reply":
                  if(isset($_GET['id']) == FALSE) {
                     header("Location: " . $config_basedir .   "/newtopic.php");
                  }
                  else {
                     header("Location: " . $config_basedir .   "/newtopic.php?id=" . $_GET['id']);
                  }
                  break;
               
               default:
                  header("Location: " . $config_basedir);
                  break;
            }
         }
         else {
            require("header.php");
            echo "This account is not verified yet. You were emailed a link
            to verify the account. Please click on the link in the email to
            continue.";
            }
            echo "This account is not verified yet. You were emailed a link
            to verify the account. Please click on the link in the email to
            continue.";
      }
      else {
         header("Location: " . $config_basedir . "/login.php?error=1");
      }
   }
   else {
      require("header.php");
   
      if($_GET['error']) {
         echo "Incorrect login, please try again!";
      }

?>
         <form action="<?php echo pf_script_with_get($SCRIPT_NAME); ?>" method="post">
         Don't have an account? Go and <a href="register.php">Register</a>!
<?php
}
   require("footer.php");
?>

Also, yes. Comments.
Also, also, yes. Duplicate lines.
Sorry, I was too lazy busy to make any of those other changes but, I think Gatito already cover them pretty well.
"Better to keep your mouth shut and be thought a fool than to open it and remove all doubt."
"red = changed"
User avatar
kujinR
Poster
Poster
 
Posts: 270
Joined: Thu Jul 29, 2010 4:39 am
Blog: View Blog (0)



Return to Interpreted Languages

Who is online

Users browsing this forum: No registered users and 0 guests