 |
11-06-2009, 07:41 PM
|
Simplifying a script
|
Posts: 109
Name: Not Telling
|
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.
|
|
|
|
11-06-2009, 07:45 PM
|
Re: Simplifying a script
|
Posts: 22,214
Location: Blackpool. UK
|
Take out all the line breaks and blank lines, that will shorten it
|
|
|
|
11-06-2009, 07:48 PM
|
Re: Simplifying a script
|
Posts: 109
Name: Not Telling
|
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.
|
|
|
|
11-06-2009, 07:50 PM
|
Re: Simplifying a script
|
Posts: 109
Name: Not Telling
|
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.
|
|
|
|
11-06-2009, 07:52 PM
|
Re: Simplifying a script
|
Posts: 22,214
Location: Blackpool. UK
|
Why?
It does what it does and it's as long as it needs to be.
|
|
|
|
11-06-2009, 07:55 PM
|
Re: Simplifying a script
|
Posts: 109
Name: Not Telling
|
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.
|
|
|
|
11-06-2009, 08:54 PM
|
Re: Simplifying a script
|
Posts: 3,176
Name: Thierry
Location: I'm the uber Spaminator !
|
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.
|
|
|
|
11-06-2009, 09:26 PM
|
Re: Simplifying a script
|
Posts: 109
Name: Not Telling
|
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.
|
|
|
|
11-07-2009, 04:47 AM
|
Re: Simplifying a script
|
Posts: 22,214
Location: Blackpool. UK
|
and line 10 would happen to be?
|
|
|
|
11-07-2009, 04:50 PM
|
Re: Simplifying a script
|
Posts: 3,176
Name: Thierry
Location: I'm the uber Spaminator !
|
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..
|
|
|
|
11-07-2009, 07:42 PM
|
Re: Simplifying a script
|
Posts: 330
Name: Mattias Nordahl
Location: Sweden
|
Quote:
Originally Posted by tripy
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
|
|
|
|
11-08-2009, 10:12 AM
|
Re: Simplifying a script
|
Posts: 3,176
Name: Thierry
Location: I'm the uber Spaminator !
|
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.
|
|
|
|
11-08-2009, 03:36 PM
|
Re: Simplifying a script
|
Posts: 330
Name: Mattias Nordahl
Location: Sweden
|
Quote:
Originally Posted by tripy
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
|
|
|
|
11-08-2009, 04:02 PM
|
Re: Simplifying a script
|
Posts: 3,176
Name: Thierry
Location: I'm the uber Spaminator !
|
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.
|
|
|
|
|
« Reply to Simplifying a script
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|
|