else if

 
Post new topic   Reply to topic    Aprelium Forum Index -> PHP
View previous topic :: View next topic  
Author Message
mrpaul
-


Joined: 23 Sep 2005
Posts: 18

PostPosted: Tue Sep 27, 2005 10:20 pm    Post subject: else if Reply with quote

Hey, i have made a news script that adds news to a page when submitted by a form, it check the passowrd witch works and then if it's wrong then it logs the ip witch is good, but if it's right it adds the news then logs the ip and thats not what i want,
the code,
<?php


$address = $_SERVER['REMOTE_ADDR'];
$addnews = "news.htm";
$news = $_POST['news'];
$title = $_POST['title'];
$name = $_POST['name'];
$pass = $_POST['password'];
$password = "addme";
$time = date ("H:i dS F");
$filey = fopen ("wrongpassowrds.html", "a+");

if ($pass == $password)
{

$file = fopen ( $addnews, "w") ;
fwrite ( $file, "<h3>$title</h3> <p> $news <p><b>Posted by, $name on $time" ) ;
fclose ( $file ) ;
echo ( " Thank you for your news $name if ther are any errors in the spelling click back nwo to edit them, <p> <h3>$title</h3> <p> $news <p><b>Posted by, $name on $time" );
}

# This is what it dose regardless of the password

else if ( $address != null);
{ fwrite ($filey, "<b>Time and date:</b> $time<br>") ; }
{ fwrite ($filey, "<b>IP Address:</b> $address<br><br><hr>") ;}
echo (" Wrong password! your ip ($address) the date and time ($time) have been logged. ");


?>
anyone know whats up?
Back to top View user's profile Send private message
roganty
-


Joined: 08 Jun 2004
Posts: 357
Location: Bristol, UK

PostPosted: Tue Sep 27, 2005 11:00 pm    Post subject: Re: else if Reply with quote

Code:
<?php
$address = $_SERVER['REMOTE_ADDR'];
$addnews = "news.htm";
$news = $_POST['news'];
$title = $_POST['title'];
$name = $_POST['name'];
$pass = $_POST['password'];
$password = "addme";
$time = date ("H:i dS F");
$filey = fopen ("wrongpassowrds.html", "a+");

if ($pass == $password)
{
$file = fopen ( $addnews, "w") ;
fwrite ( $file, "<h3>$title</h3> <p> $news <p><b>Posted by, $name on $time" ) ;
fclose ( $file ) ;
echo ( " Thank you for your news $name if ther are any errors in the spelling click back nwo to edit them, <p> <h3>$title</h3> <p> $news <p><b>Posted by, $name on $time" );
}

# This is what it dose regardless of the password

else if ( $address != null);
{ fwrite ($filey, "<b>Time and date:</b> $time<br>") ; }
{ fwrite ($filey, "<b>IP Address:</b> $address<br><br><hr>") ;}
echo (" Wrong password! your ip ($address) the date and time ($time) have been logged. ");


?>


arn't you getting any errors at all? because that last bit is full of them
revised code:
Code:
<?php
$address = $_SERVER['REMOTE_ADDR'];
$addnews = "news.htm";
$news = $_POST['news'];
$title = $_POST['title'];
$name = $_POST['name'];
$pass = $_POST['password'];
$password = "addme";
$time = date ("H:i dS F");
$filey = fopen ("wrongpassowrds.html", "a+"); //nice spelling ;)

if ($pass == $password)
{
$file = fopen ( $addnews, "w") ;
fwrite ( $file, "<h3>$title</h3> <p> $news <p><b>Posted by, $name on $time" ) ;
fclose ( $file ) ;
echo ( " Thank you for your news $name if ther are any errors in the spelling click back nwo to edit them, <p> <h3>$title</h3> <p> $news <p><b>Posted by, $name on $time" ); //nwo?
}

//changed from now

else if ( $address != null)
{ fwrite ($filey, "<b>Time and date:</b> $time<br>") ;
 fwrite ($filey, "<b>IP Address:</b> $address<br><br><hr>") ;
echo (" Wrong password! your ip ($address) the date and time ($time) have been logged. ");
}

?>


all i did was remove one ";" two "}" and one "{" and added a "}"
simple!
_________________
Anthony R

Roganty
| Links-Links.co.uk
Back to top View user's profile Send private message Visit poster's website
mrpaul
-


Joined: 23 Sep 2005
Posts: 18

PostPosted: Tue Sep 27, 2005 11:26 pm    Post subject: Reply with quote

Great thanks it works, i always get mixed up with { and ; what dose { something } mean exatly?
and the errors, lol what can i say? i did post because i need help with it.
and the spelling is a just a typeo i dont usealy spell now like nwo.
Back to top View user's profile Send private message
AbyssUnderground
-


Joined: 31 Dec 2004
Posts: 3855

PostPosted: Wed Sep 28, 2005 4:38 pm    Post subject: Reply with quote

{ and } are the parts of the script that you want to assign to the function. As an example using an if...

Quote:
if($variable=="hello"){
//opened what the if should do if it equals 'hello'
echo("hello");
// the ; means end the current command. This is needed unless you want errors
}
//end what the if should do if it equals 'hello'


Basically it defines what the if should do. Anything after the end } will be ignored by the IF because it is not a part of what it should do.

semi-colons ( ; ) just end the command you are using. So in an echo, the semi-colon tells the interpreter than the command 'echo' has been stopped for a new command to be started,



I hope this is a good enough explanation. Maybe someone else has a better one. Ive never been very good at explaining things :-)
_________________
Andy (AbyssUnderground) (previously The Inquisitor)
www.abyssunderground.co.uk
Back to top View user's profile Send private message Visit poster's website
Display posts from previous:   
Post new topic   Reply to topic    Aprelium Forum Index -> PHP All times are GMT + 1 Hour
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB phpBB Group