Jump to content

How Does This Code Work??


delirium

Recommended Posts

Hey, im starting to get really into php but im not completely familiar with all the code yet.

 

I had my cousin type this up for my login system and its a comment/Guestbook thing.

 

It works but im really not sure how and i would love to understand whats happening in this code. Could anyone please go through the code and explain whats happening?

 

The code is below, but here is an example of what im looking for..

 

$message = htmlspecialchars(addslashes("$_POST[message]"));

**This sets the variable "$message"**

 

Ideally i would like en explanation like that for every line, thanks so much in advance.

 

 

______________________________________________________________________________

//submit comment

 

switch ($_GET['page'])

{ case 2:

 

if(isset($_GET["sign"]))

{

 

$message = htmlspecialchars(addslashes("$_POST[message]"));

$time = date("F j, Y, g:i a");

$querty = mysql_query("INSERT INTO guestbook(owner,postedby,post,time) VALUES('$req_user','$session->username','$message','$time')");echo "

Your

 

message has been posted

"; }

 

else {echo ( "Message was not posted" ); // or no request sent

}

 

break;

 

}

 

// Display comment system

 

$req_user = trim($_GET['user']);

$getcomments = mysql_query( "SELECT * FROM `guestbook` WHERE `owner` = '$req_user' ORDER BY `ID` DESC" );

 

while ($comments = mysql_fetch_array($getcomments))

{

echo "$comments[postedby] ($comments[time]) - $comments

";

}

 

//Display comment submit box

 

echo "


Sign $req_user's guestbook

 


";

 

/* Link back to main */

echo "

Back To [Main]

";

 

?>

 

This is the line i really dont get,

 

What page is that? i have a userinfo.php but what all the ?page= stuff.. i know its a HUGE part of php but i'm really new to this..

Edited by delirium
Link to comment
Share on other sites

Some of this is a bit hard to describe, partially because the code you have posted appears to be code snippets, rather than the whole thing. Still, you should be able to get a good idea of what is going on.

 

I've separated out the code into a couple different chunks that do different things...

 

This chunk of code takes the input from a form and saves it in the database:

switch ($_GET['page']) //this sets up a switch statement, checking for various options. In this case, it gets the value of the "page" variable from the url (filename.php?page=2). If it is "2" it does the code within the "case 2:" block
{     
   case 2: 

   if(isset($_GET["sign"])) //checks if "sign" is set in the url, and gets the value using $_GET[] (if "sign" is set in the url, it will look something like this: filename.php?sign=value)
   {
       //sets the message variable to the value of the "message" field in the form. htmlspecialchars() and addslashes() are both functions that are intended to help protect against hacking or invalid inputs
       $message = htmlspecialchars(addslashes("$_POST[message]"));

       //sets the time variable to the current time, in the specified format
       $time = date("F j, Y, g:i a");

       //this is a mysql query that saves the data captured within the form to the database
       $querty = mysql_query("INSERT INTO guestbook(owner,postedby,post,time) VALUES('$req_user','$session->username','$message','$time')");

       //displays a message to the user letting him/her know that the comment has been posted.
       echo "Your message has been posted
"; 
   }
   else
   // if the "sign" variable is not set in the URL, show an error 
   {
       echo ( "Message was not posted" ); // or no request sent
   } 

   // end the switch statement
   break;

}

 

This chunk of code displays the comments (it appears like this only shows comments of the specified user), getting the information from the database

//sets the req_user variable based on the URL
$req_user = trim($_GET['user']);

// this is a mysql query that gets the comments from the database
$getcomments = mysql_query( "SELECT * FROM `guestbook` WHERE `owner` = '$req_user' ORDER BY `ID` DESC" );

// this loops through all of the results, repeating the code within the loop until there are no more results 
while ($comments = mysql_fetch_array($getcomments))
{
   // this displays the comment from the database
   echo "$comments[postedby] ($comments[time]) - $comments[post] 
";
}

 

 

This displays a form, allowing users to comment. There really isn't anything super complicated about this...

//displays header text
echo "
Sign $req_user's guestbook


// sets up a form



";

/* Link back to main */
echo "
Back To [Main]
";

 

This is the line i really dont get,

When the form is submitted, it goes to the page that is specified within the action perimeter. In this case, it is the userinfo.php page, setting additional variables (in this case, "page" = 2, "user" = the value of the $req_user variable, "sign" = the value of the $req_user variable, and "from" = the username set within the session object (this is a bit over your head... sorry) are the variables) in the URL that will be used by PHP when processing the form.

Link to comment
Share on other sites

Realistically, I probably wouldn't start learning PHP from such a complicated example as this. You might want to consider taking a look at some of the basic articles on http://www.tizag.com/phpT/ or doing a couple of the (free) video tutorials on http://phpvideotutorials.com/free to get a basic understanding of PHP, and then come back to this. It'll make a lot more sense then.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...