Memory problem in PHP simple HTML DOM
Allowed memory size of 67108864 bytes exhausted,
and this is because simple HTML dom don't free up the memory in real time, so the solution is:
Each time you create a dom object ( foe exemple using: $html = file_get_html("http://someurl/"); or str_get_html... ), then when you don't need it anymore you have to call _destruct:
$html->_destruct();
unset($html);
“Who Is Online” Widget With PHP, MySQL & jQuery
I hope you will like it :)
String Capitalization Functions: strtoupper-strtolower-ucwords.
$originalString = "Testing string Capitalization AbCDe";
$upperCase = strtoupper($originalString); // Result: TESTING STRING CAPITALIZATION ABCDE
$lowerCase = strtolower($originalString); // Result: testing string capitalization abcde
$ucTitleString = ucwords($originalString); // Result: Testing String Capitalization Abcde
?>
This does explain everything about how to manipulate capitalization. In fact strtoupper returns the string in upper case, strtolower converts to lower case, and ucwords capitalize the first letter of each word, without making any changes to other characters in the word. (ie tEsT becomes TEsT with ucwords).
If you have any question, I'm ready to help you.
PHP For Loop
This is how:
for ($userid=1;$userid<=10;$userid++){
send_email($userid);
}
?>
Where send_email() is your own function that send the email to the user with the id as parameter.
Another exemple to For Loop, let's say you want to count 1+2x2+3x3+...+nxn, this is how you can do it:
$n=10;
$t=0;
for ($i=1;$i<=$n;$i++){
$t+=$i*$i; // the same as $t=$t+$i*$i
}
?>
So how For loop works ?
1. Set a counter variable to some initial value (i.e $i=0).
2. Check to see if the conditional statement is true(i.e $i<=10).
3. Execute the code within the loop.
4. Increment a counter at the end of each iteration through the loop(i.e $i++, or $i+=1, ou $i=$i+1) .
And this is how to do it:
do this code;
}
The php loop will help you a lot in php programming, and there are other ways to do loops other that For, like while.
PHP Validate Email Address: Email Validator
Most of people who are new to php world, just verify if the text box of the email address is filled or not, without verifying the syntax of the email address...
Something like this they use:
But when you want to develop a php website in the best way, taking care of all little details, you should think what happens when your user does make a mistake in the email address, writing something like this: his_email@gmailcom, or his_emailgmail.com or even he writes ssdfsff (like many people do), I'm sure you would like to inform him and ask him to correct his email address, you maybe also want to check if the domain name of his email address is valid, you can't accept an email like this: blahblah@dfsjhbhjjqsdqq.fdf .
So here is the php script I think you should use everytime you have a form with 'email address' input :
function validate_email($email) {
//check for all the non-printable codes in the standard ASCII set,
//including null bytes and newlines, and exit immediately if any are found.
if (preg_match("/[\\000-\\037]/",$email)) {
return false;
}
$pattern = "/^[-_a-z0-9\'+*$^&%=~!?{}]++(?:\.[-_a-z0-9\'+*$^&%=~!?{}]+)*+@(?:(?![-.])[-a-z0-9.]+(?<![-.])\.[a-z]{2,6}|\d{1,3}(?:\.\d{1,3}){3})(?::\d++)?$/iD";
if(!preg_match($pattern, $email)){
return false;
}
// Validate the domain exists with a DNS check
// if the checks cannot be made (soft fail over to true)
list($user,$domain) = explode('@',$email);
if( function_exists('checkdnsrr') ) {
if( !checkdnsrr($domain,"MX") ) { // Linux: PHP 4.3.0 and higher & Windows: PHP 5.3.0 and higher
return false;
}
}
else if( function_exists("getmxrr") ) {
if ( !getmxrr($domain, $mxhosts) ) {
return false;
}
}
return true;
} // end function validate_email
?>
Now you can use this function with the email address you want to verify as parameter, and it will return you true if it valide and false if it is not.
This nice function will not know if an email exist really or not, it just verifies the syntax and the domain name, for example will not know that qsddsfqsdsdfqsdsqdsqd@gmail.com is not valide, but knows that qsqsdsq@dfsfsqdqsdqd.com is an invalid email address .
I hope this help you a lot.
Please leave comments or link to us if you think this is usefull :)
PHP Random code Generator
So, this is it:
function createRandomPassword() {
$chars = "abcdefghijkmnopqrstuvwxyz023456789";
srand((double)microtime()*1000000);
$i = 0;
$pass = '' ;
while ($i <= 9) {
$num = rand() % 33;
$tmp = substr($chars, $num, 1);
$pass = $pass . $tmp;
$i++;
}
return $pass;
}
?>
That's it!
There are many options you can play with, for example you can choose the characters to generate code from: $chars ="1234567890" will generate numeric code; $chars = "abcdefghijkmnopqrstuvwxyz023456789"; will generate alphanumeric codes. In fact the code will be generated randomly from the list of characters in $chars.
You can also change the lenght of the generated code by changing the value in "while ($i <= 9)", 9 here means you will get a code with 10 characters, as this counter (while) starts from 0 to 9. If you do "while ($i <= 7)" you will get 8 characters code.
This is the easiest and the best way to generate random code from php. I will always provide the best PHP scrips :)
If you like my blog, please give a link to us from your website, I will appreciate that :)
PHP Get browser Language
You can get the language of your visitor in this way:
This will give you the 2letter language code, ie: en for english and fr for french .
In fact $_SERVER['HTTP_ACCEPT_LANGUAGE'] will get all language the user prefer to use, in order. So substr() will give you the first choice, you can also get the second choice, the third ...
Having the language of your visitor will let you display custom page depending on his language, for example if someone's language is french, you will have $lang="fr" , then you can redirect him to www.yoursite.com/fr/ or yoursite.com/?lang=fr to show him your website in french, or show him things related to french language, whatever your website is about.
PHP $_REQUEST
But what if you want your php to work for both post and get datas in the same time, to precess the form whatever it was in post method or get method, you will then think to use if conditions to check whether of two methods was used:
if (isset($_POST['var']))
// process data...
if (isset($_GET['var']))
// process data in the same way...
BUT there is a little and usefull solution: $_REQUEST, because $REQUEST can get any form field value, whatever it was GET or POST method, it does not care.
This means that $_REQUEST['var'] = $_POST['var'] if the form was in post method, and $_REQUEST['var'] = $_GET['var'] if the form was get. ('var' is the field name you want to get its value)
So now you will not care what method the form use :)
You can also use isset() with $_REQUEST, for example:
if (isset($_REQUEST['var'])) is the same than if ( isset($_POST['var']) or isset($_GET['var']) )
I hope this little tip will help you, we will provide best php tips, scripts as well as php tutorials.
PHP send mail SMTP authentification
You can choose the smtp server, use your username and password, and can send a html email.
This script does not use php mail() function, it's all from scrach.
function authSendEmail($from, $namefrom, $to, $nameto, $subject, $message)
{
$smtpServer = "smtpout.europe.secureserver.net";
$port = "25";
$timeout = "30";
$username = "username@mailserver.com";
$password = "mypassword";
$localhost = "localhost"; //hosting server
$newLine = "\r\n";
$smtpConnect = fsockopen($smtpServer, $port, $errno, $errstr, $timeout);
$smtpResponse = fgets($smtpConnect, 515);
if(empty($smtpConnect))
{
$output = "Failed to connect: $smtpResponse";
return false;
} else {
$logArray['connection'] = "Connected: $smtpResponse";
}
//Request Auth Login
fputs($smtpConnect,"AUTH LOGIN" . $newLine);
$smtpResponse = fgets($smtpConnect, 515);
//Send username
fputs($smtpConnect, base64_encode($username) . $newLine);
$smtpResponse = fgets($smtpConnect, 515);
//Send password
fputs($smtpConnect, base64_encode($password) . $newLine);
$smtpResponse = fgets($smtpConnect, 515);
//Say Hello to SMTP
fputs($smtpConnect, "HELO $localhost" . $newLine);
$smtpResponse = fgets($smtpConnect, 515);
//Email From
fputs($smtpConnect, "MAIL FROM: $from" . $newLine);
$smtpResponse = fgets($smtpConnect, 515);
//Email To
fputs($smtpConnect, "RCPT TO: $to" . $newLine);
$smtpResponse = fgets($smtpConnect, 515);
//The Email
fputs($smtpConnect, "DATA" . $newLine);
$smtpResponse = fgets($smtpConnect, 515);
//Construct Headers
$headers = "MIME-Version: 1.0" . $newLine;
$headers .= "Content-type: text/html; charset=iso-8859-1" . $newLine;
//Add these two lines if you have any problem
//$headers .= "To: $nameto <$to>" . $newLine;
//$headers .= "From: $namefrom <$from>" . $newLine;
fputs($smtpConnect, "To: $nameto <$to>\nFrom: $namefrom <$from>\nSubject: $subject\n$headers\n\n$message\n.\n");
$smtpResponse = fgets($smtpConnect, 515);
// Say Bye to SMTP
fputs($smtpConnect, "QUIT" . $newLine);
$smtpResponse = fgets($smtpConnect, 515);
return true;
}
?>
ob_start - save php output to a string - The PHP output buffering
The PHP output buffering will save all the server outputs ( html and php prints) to a string variable.
So to start buffering, use ob_start(); this will keep saved any output.
Then you use $variable = ob_get_clean(); to stop buffering, and copy the buffer content to the variable.
Here are few samples of the use of ob_start() and ob_get_clean()
<?php ob_start(); //Turn on output buffering ?>
Hello world, <a href="http://www.blogger.com/myotherpage.php">link</a>
<div class="style">Content</div>
<?php $var = ob_get_clean(); //copy current buffer contents into $message variable and delete current output buffer ?>
Now the content of the variable $var should be:
Hello word, <a href="http://www.blogger.com/myotherpage.php">link</a>
<div class="style">Content</div>
Another example (with php outputs buffered)
<?php ob_start(); ?>
Hello world, <?php echo "My Website"; ?>
Thank you.
<?php $var = ob_get_clean(); ?>
And this will be the content of $var:
Thank you.
That's all !
PHP send HTML email, The complete php mail sending script
If you want a complete mail sending script, that send in html, this is what you have to use:
//here the receiver of the email
$to = 'receiver@example.com';
//here the subject of the email
$subject = 'Test HTML email';
//create a boundary string. It must be unique
//so we use the MD5 algorithm to generate a random hash
$random_hash = md5(date('r', time()));
//define the headers we want passed. Note that they are separated with \r\n
$headers = "From: myemail@mysite.com\r\nReply-To: myemail@mysite.com";
//add boundary string and mime type specification
$headers .= "\r\nContent-Type: multipart/alternative; boundary=\"PHP-alt-".$random_hash."\"";
//define the body of the message.
$message ='
--PHP-alt-'.$random_hash.'
Content-Type: text/html; charset="iso-8859-1"
Content-Transfer-Encoding: 7bit
<h2>Hello World!</h2>
<p>This is something with <b>HTML</b> formatting.</p>
--PHP-alt-'.$random_hash.'--';
//send the email
$mail_sent = @mail( $to, $subject, $message, $headers );
//if the message is sent successfully print "Mail sent". Otherwise print "Mail failed"
echo $mail_sent ? "Mail sent" : "Mail failed";
?>
The script is with comments, to tell you what exactly each instruction does :)
So as you see, we added:
Content-Type: text/html; charset="iso-8859-1"
to the message body ($message), this way the mail client of the receiver will know that the email is in html.
There are also few other changements that are nor really very necessary like the boundary string, but it is better to use them :)
PHP mail() - send email with php
You need to send email using php when you have a comment or contact us form, where your visitors contact you via a web page form, and you get the message to your email address, or when you have a signup / registration form and you want to send automatically a confirmation link or a welcome message to your new users.
This is how you send email using php:
<?php $TO = "someone@something.com"; $h = "From: me@mywebsite.com"; $message = "my message here"; mail($TO, $subject, $message, $h); Header("Location: http://mysite.com/thankyou.html"); ?>
That's it ! mail() function have 3 required parameters and one optional.
$To is the receiver email address, $subject is the subject and $message is the body of your message.
$h is optional, is the header you will send, it contains the sender email address ("FROM: me@mysite.com")
I will post next how to send a html email, and how to use SMTP Authentication which means how to choose the smtp server and use your username and password to use that server.
Search With Google
Categories
- 1-General (3)
- 2-Getting started (4)
- 3-Strings (10)
- 4-Server config (1)
- 5-Best scripts (7)
- email (4)
- files (6)
- Forms (3)
- If statements (3)
- Loop statements (2)
- Mysql (2)
- Server variables (3)
- Sessions (1)
- Tutorials-planet.com (1)
- web hosting (1)
- You need a programmer ? (1)
Links to visit !
Blog Archive
-
▼
2010
(12)
-
►
January
(10)
- String Capitalization Functions: strtoupper-strtol...
- PHP For Loop
- PHP Validate Email Address: Email Validator
- PHP Random code Generator
- PHP Get browser Language
- PHP $_REQUEST
- PHP send mail SMTP authentification
- ob_start - save php output to a string - The PHP o...
- PHP send HTML email, The complete php mail sending...
- PHP mail() - send email with php
-
►
January
(10)