Reply
Old 11-06-2009, 07:41 PM Simplifying a script
Super Talker

Posts: 109
Name: Not Telling
Trades: 0
I currently have this script:

PHP Code:
<?php
$date2 
date("F j Y");
$ip $_SERVER['REMOTE_ADDR'];
require(
"inc/config.php");
$sql="INSERT INTO `accounts` (username, password, ip, addeddate)
VALUES('$_POST[Username]','$_POST[Password]','$ip','$date2')"
;
if (!
mysql_query($sql))
{
die(
'Error: ' mysql_error());
}
echo 
"";
$result mysql_query("SELECT email FROM admin WHERE id = '1'");
if (!
$result
{
    echo 
'Could not run query: ' mysql_error();
    exit;
}

$row mysql_fetch_row($result);
$to $row[0];
$qcheck "SELECT * FROM admin";
$rcheck mysql_query($qcheck) or die("<B>MySQL error! Make sure you edited the config file! <BR><BR> Error: ".mysql_error());
$frow mysql_fetch_array($rcheck);


if (
$frow['sendemail'] == "1")
{
$subject "New Registered User";
$from "test";
$message "
A new user has signed up and has been added to the database
Username: $_POST[Username] 
Password: $_POST[Password]
IP Address: $ip
Date: $date2
"
;
$headers "From: $to";
$sent mail($to$subject$message$headers) ;
    echo 
"sent";
}
else 
{    
echo 
"not sent - since not enabled";
}
?>
It stores the information in a database and then sends me an email.

You can choose if you want to send the email with
Code:
if ($frow['sendemail'] == "1")
if its 1 then send, if 2 or anything else then don't send.

I was wondering if things can be taken out or combined to shorten the script.

I am not sure what to do.

To me it seems very long for something so simple.

Thank You.
__________________
MY MSN: Sith717@Hotmail.com
PHP, HTML, and CSS Coding, Logo and Web Design - Professionally done.
PM me anytime for HTML, PHP or web design help. I will be glad to help you out.
sith717 is offline
Reply With Quote
View Public Profile
 
 
When You Register, These Ads Go Away!
Old 11-06-2009, 07:45 PM Re: Simplifying a script
chrishirst's Avatar
Super Moderator

Posts: 22,214
Location: Blackpool. UK
Trades: 0
Take out all the line breaks and blank lines, that will shorten it
__________________
Chris. ->> Links are advertising NOT optimising!! <<-
Growing old is mandatory - Growing up is optional
Code Samples | People Counting System | Bits & Bobs
chrishirst is online now
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 11-06-2009, 07:48 PM Re: Simplifying a script
Super Talker

Posts: 109
Name: Not Telling
Trades: 0
Thats it?

Is there anything I can do to simplify it, like combine things?
__________________
MY MSN: Sith717@Hotmail.com
PHP, HTML, and CSS Coding, Logo and Web Design - Professionally done.
PM me anytime for HTML, PHP or web design help. I will be glad to help you out.
sith717 is offline
Reply With Quote
View Public Profile
 
Old 11-06-2009, 07:50 PM Re: Simplifying a script
Super Talker

Posts: 109
Name: Not Telling
Trades: 0
Okay I took out a few things, any more things that I can do?

Current code:

PHP Code:
<?php
$date2 
date("F j Y");
$ip $_SERVER['REMOTE_ADDR'];
require(
"inc/config.php");
$sql="INSERT INTO `accounts` (username, password, ip, addeddate)
VALUES('$_POST[Username]','$_POST[Password]','$ip','$date2')"
;

$result mysql_query("SELECT email FROM admin WHERE id = '1'");

$row mysql_fetch_row($result);
$to $row[0];
$qcheck "SELECT * FROM admin";
$rcheck mysql_query($qcheck) or die("<B>MySQL error! Make sure you edited the config file! <BR><BR> Error: ".mysql_error());
$frow mysql_fetch_array($rcheck);

if (
$frow['sendemail'] == "1")
{
$subject "New Registered User";
$from "test";
$message "
A new user has signed up and has been added to the database
Username: $_POST[Username] 
Password: $_POST[Password]
IP Address: $ip
Date: $date2
"
;
$headers "From: $to";
$sent mail($to$subject$message$headers) ;
    echo 
"sent";
}
else 
{    
echo 
"not enabled";
}
?>
__________________
MY MSN: Sith717@Hotmail.com
PHP, HTML, and CSS Coding, Logo and Web Design - Professionally done.
PM me anytime for HTML, PHP or web design help. I will be glad to help you out.
sith717 is offline
Reply With Quote
View Public Profile
 
Old 11-06-2009, 07:52 PM Re: Simplifying a script
chrishirst's Avatar
Super Moderator

Posts: 22,214
Location: Blackpool. UK
Trades: 0
Why?

It does what it does and it's as long as it needs to be.
__________________
Chris. ->> Links are advertising NOT optimising!! <<-
Growing old is mandatory - Growing up is optional
Code Samples | People Counting System | Bits & Bobs
chrishirst is online now
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 11-06-2009, 07:55 PM Re: Simplifying a script
Super Talker

Posts: 109
Name: Not Telling
Trades: 0
Okay. I didn't know, I just thought there are some extra things. :P
__________________
MY MSN: Sith717@Hotmail.com
PHP, HTML, and CSS Coding, Logo and Web Design - Professionally done.
PM me anytime for HTML, PHP or web design help. I will be glad to help you out.
sith717 is offline
Reply With Quote
View Public Profile
 
Old 11-06-2009, 08:54 PM Re: Simplifying a script
tripy's Avatar
Do not try this at home!

Posts: 3,176
Name: Thierry
Location: I'm the uber Spaminator !
Trades: 0
Making a script shorter is not opimizing it.
It generally makes it less readable at first sight.

I gave it a try here, in 2 minutes:
PHP Code:
<?php
require($_SERVER['DOCUMENT_ROOT']."/inc/config.php");
$date2 date("F j Y");
$ip $_SERVER['REMOTE_ADDR'];
function 
query($sql){
  return 
mysql_query($sql) or die("Could not run query: ".mysql_error());
}
query("INSERT INTO `accounts` (username, password, ip, addeddate) VALUES('$_POST[Username]','$_POST[Password]','$ip','$date2')");
echo 
"";
$to mysql_fetch_row(query("SELECT email FROM admin WHERE id = '1'"))[0];
if(
mysql_result(query("SELECT * FROM admin"),0,'sendemail')==1{
  
$subject "New Registered User";
  
$from "test";
  
$message = <<<txt
A new user has signed up and has been added to the database
Username: 
{$_POST['Username']}
Password: 
{$_POST['Password']}
IP Address: $ip
Date: $date2
txt;
  
$headers "From: $to";
  
$sent mail($to$subject$message$headers) ;
  echo 
"sent";
}else {
  echo 
"not sent - since not enabled";
}
?>
26 lines in place of 44.
But for the PHP engine itself, it will take the same time to parse this than the 44 lines long script.
And you might find it harder to read, as sometimes functions are becoming nested
PHP Code:
if(mysql_result(query("SELECT * FROM admin"),0,'sendemail')==1{
  ...

__________________
Only a biker knows why a dog sticks his head out the window.
tripy is offline
Reply With Quote
View Public Profile Visit tripy's homepage!
 
Old 11-06-2009, 09:26 PM Re: Simplifying a script
Super Talker

Posts: 109
Name: Not Telling
Trades: 0
I get this error:

Quote:
Parse error: syntax error, unexpected '[' in /home/cherdak/public_html/email.php on line 10
__________________
MY MSN: Sith717@Hotmail.com
PHP, HTML, and CSS Coding, Logo and Web Design - Professionally done.
PM me anytime for HTML, PHP or web design help. I will be glad to help you out.
sith717 is offline
Reply With Quote
View Public Profile
 
Old 11-07-2009, 04:47 AM Re: Simplifying a script
chrishirst's Avatar
Super Moderator

Posts: 22,214
Location: Blackpool. UK
Trades: 0
and line 10 would happen to be?
__________________
Chris. ->> Links are advertising NOT optimising!! <<-
Growing old is mandatory - Growing up is optional
Code Samples | People Counting System | Bits & Bobs
chrishirst is online now
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 11-07-2009, 04:50 PM Re: Simplifying a script
tripy's Avatar
Do not try this at home!

Posts: 3,176
Name: Thierry
Location: I'm the uber Spaminator !
Trades: 0
Try a bit harder. I haven't done PHP for the last 4 years, and I have typed this directly in the reply box.
What I gave you was not tested.
change
PHP Code:
if(mysql_result(query("SELECT * FROM admin"),0,'sendemail')==1
to
PHP Code:
if(mysql_result(query("SELECT * FROM admin"),0,'sendemail')==1){ 
to correct this error.
__________________
Only a biker knows why a dog sticks his head out the window.

Last edited by tripy; 11-07-2009 at 04:52 PM..
tripy is offline
Reply With Quote
View Public Profile Visit tripy's homepage!
 
Old 11-07-2009, 07:42 PM Re: Simplifying a script
lizciz's Avatar
Ultra Talker

Posts: 330
Name: Mattias Nordahl
Location: Sweden
Trades: 0
Quote:
Originally Posted by tripy View Post
Try a bit harder. I haven't done PHP for the last 4 years, and I have typed this directly in the reply box.
What I gave you was not tested.
change
PHP Code:
if(mysql_result(query("SELECT * FROM admin"),0,'sendemail')==1
to
PHP Code:
if(mysql_result(query("SELECT * FROM admin"),0,'sendemail')==1){ 
to correct this error.
Seems both of your code snippets are the same
Besides, the error message mentioned a '['.

Sith, please show the line where the error occurs.
__________________
596f75206d65616e20796f752063616e2061637475616c6c79 207265616420746869733f
lizciz is online now
Reply With Quote
View Public Profile Visit lizciz's homepage!
 
Old 11-08-2009, 10:12 AM Re: Simplifying a script
tripy's Avatar
Do not try this at home!

Posts: 3,176
Name: Thierry
Location: I'm the uber Spaminator !
Trades: 0
Quote:
Seems both of your code snippets are the same
Nope, I had forgot a closing ) in the first, that exists in the second.
But it might be another error, yes. As I said, I did this without php at hand.
__________________
Only a biker knows why a dog sticks his head out the window.
tripy is offline
Reply With Quote
View Public Profile Visit tripy's homepage!
 
Old 11-08-2009, 03:36 PM Re: Simplifying a script
lizciz's Avatar
Ultra Talker

Posts: 330
Name: Mattias Nordahl
Location: Sweden
Trades: 0
Quote:
Originally Posted by tripy View Post
Nope, I had forgot a closing ) in the first, that exists in the second.
Darn, I missed that one, evan though I went through it like 30 times
Guess I was struck by blindness. Speaking of blindness, check this out (the fist 1.30 minutes) http://www.youtube.com/watch?v=RkGYoC33MMs
__________________
596f75206d65616e20796f752063616e2061637475616c6c79 207265616420746869733f
lizciz is online now
Reply With Quote
View Public Profile Visit lizciz's homepage!
 
Old 11-08-2009, 04:02 PM Re: Simplifying a script
tripy's Avatar
Do not try this at home!

Posts: 3,176
Name: Thierry
Location: I'm the uber Spaminator !
Trades: 0
Ok, now I just mashed this through the php engine, and I see where is the problem on the line 10...
Darn I miss python when I come back to PHP...
it's the "[0]" in the line:
PHP Code:
$to mysql_fetch_row(query("SELECT email FROM admin WHERE id = '1'"))[0]; 
You have to do it in 2 lines:
PHP Code:
$to mysql_fetch_row(query("SELECT email FROM admin WHERE id = '1'"));
$to $to[0]; 
@Mattias:
He, good one. I did missed the engine too
__________________
Only a biker knows why a dog sticks his head out the window.
tripy is offline
Reply With Quote
View Public Profile Visit tripy's homepage!
 
Reply     « Reply to Simplifying a script
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML

 



Page generated in 0.16381 seconds with 13 queries