The place that has your tutorials

Archive for the ‘PHP Coding’ Category

Flash Cookies

Sunday, July 29th, 2007

OK here is how to create a cookie in Flash backened with PHP.

Firstly we start with the Flash file.

1. Create a flash button. To do so draw a rectangle, right click on it and select make symbol. select button then press ok.
2. In the action panel of the button add the following:

Actionscript:
  1. on (release) {
  2.     cookie = "flashsite";
  3.     getURL("input.php", "", "POST");
  4. }

3. On the action for the frame add the following:

Actionscript:
  1. cookie = "value";

4. Now you have set a variable as soon as the SWF loads called cookie, when you press the button it changes to flashsite. the seconline takes you to input.php where it sees the variable and acts upon it.
5. My input.php looks like this:

PHP:
  1. <?php
  2.    
  3. $cookie = $HTTP_POST_VARS['cookie'];
  4.    
  5. if($cookie == 'flashsite')
  6. {
  7. setcookie("asgsoft_redirect", $cookie, time()+31536000);
  8. header("Location: index.php");
  9. }
  10. if($cookie == 'nonflash')
  11. {
  12. setcookie("asgsoft_redirect", $cookie, time()+31536000);
  13. header("Location: nonflash");
  14. }
  15.    
  16. if($cookie == 'skipintro')
  17. {
  18. setcookie("asgsoft_redirect", $cookie, time()+31536000);
  19. header("Location: index.php?scene2");
  20. }
  21.    
  22. ?>

6. This is part of mu upcoming site where it allows the user to select whether to see flash/nonflash site. if they select flash then whether to see the into or not.

I hope this has been helpful to somebody

Search Page

Sunday, July 29th, 2007

OK This tutorial will show you how to make a search page.

Firstly we make a form. I am going to use this:

HTML:
  1. <form method="post" action="searchpage.php">
  2. <p>Please enter keyword.</p>
  3. <input name="word" type="text" size=25 maxlength=25>
  4. </p>
  5. <input type="Submit" name="Submit" value="Submit">
  6. </p>
  7. </form>

This is the form where we will enter the keywords. Now the next step is to make the searchpage.php.

It will look like this

PHP:
  1. <?php
  2. // connect to db
  3. $db = mysql_connect("localhostt", "username", "password");
  4. mysql_select_db("dbname",$db);
  5.  
  6. //check something is sent
  7. if ($_POST['word']) {
  8. $word= $_POST["word"];
  9. // search query
  10. $result = mysql_query("SELECT * FROM `table` WHERE `field` LIKE '%$word%'");
  11. $postword = $_POST['word'];
  12. echo "Search Results for: <strong> $postword </strong> <br />";
  13. echo"<p> </p>";
  14. echo"<p> </p>";
  15. echo"<p> </p>";
  16. echo"<p> </p>";
  17. //get results
  18. while($get=mysql_fetch_array($result))
  19. {
  20.  
  21.  
  22. $date = $get['date'];
  23. $name = $get['name'];
  24. $title= $get['title'];
  25.  
  26. //show results
  27. echo("<strong>$title</strong> was added on <strong>$date</strong> by <strong>$name</strong>");
  28.  
  29. echo"<p></p>";
  30. echo"<p></p>";
  31. }
  32. // if nothing is sent
  33. }else {
  34. die ("No search word specified");
  35. }
  36.  
  37. ?>

mail()

Sunday, July 29th, 2007

A famous function, yet mis-used is mail().

Mail function can be used in several ways, eg a contact form and auto-senders suitable for confirming registration.

Ok the function is used like this:

PHP:
  1. mail($to, $subject, $message, $headers);

you can either set the variables or enter them manually like this:

PHP:
  1. mail(myemail@mycompany.com, message subject, message , headers);

Although its preferred to use variables because its easier to use and modify later.

To make sure the message was sent correctly you can use the following "if block"

PHP:
  1. if(mail($to,$subject,$message,$headers)){
  2. echo "Email Sent";
  3. }else{
  4. echo "Email Sending Failed";
  5. }

If you are making an auto-sender you add this to the page that has the email address passed in most probably by a form, so you do this:

PHP:
  1. $to="someeila@comapny.com";
  2. $from=$_POST['email'];
  3. $message="my message";
  4. $subject="Subject of message";
  5. $headers = "From: $from";
  6. if(mail($to,$subject,$message,$headers)){
  7. echo "Email Sent messaage";
  8. }else{
  9. echo "Email Sending Failed mesasage";
  10. }

The other example i mentioned at the beginning was the contact form, look at this form, it has both parts in one PHP page:

PHP:
  1. <?php
  2. if(!isset($submit)){
  3. echo "<form action='$_SERVER[PHP_SELF]' method='post'>
  4. <p>Name: <input type='text' name='name'><br><p>
  5. <p>Email: <input type='text' name='email'><br><p>
  6. Message: <br>
  7. <textarea name='message' cols='40' rows='15'></textarea><br>
  8. <input type='submit' name='submit' value='send'>
  9. </form>";
  10. }else{
  11. $to="asmgomaa@gmail.com";
  12. $from=$_POST['email'];
  13. $message=$_POST['message'];
  14. $subject="Website Contact Form";
  15. $submit=$_POST['submit'];
  16. $headers = "From: $from";
  17. if(mail($to,$subject,$message,$headers)){
  18. echo "Email Sent";
  19. }else{
  20. echo "Email Sending Failed";
  21. }
  22. }
  23. ?>

An example of it is available here .

I hope this is helpful and makes the mail() function clear and easier to use.

Date()

Sunday, July 29th, 2007

This is a very good function that can be used to date anything from last login to date adding a specific post. It is used in PHP 4 and 5. It is a relatively easy function to use because it goes like this:

PHP:
  1. date("parameters");

but its no use like this so we make it part of a variable

PHP:
  1. $date = date("parameters")

the parameters you put inside are specific and are in the table below. i got this table from the PHP official site: http://uk.php.net/date

you can also easily format time so this:

PHP:
  1. date('l dS \of F Y h:i:s A');

will give Monday 12th of November 2005 1:04:46 PM

I hope you find this helpful

Search

You are currently browsing the archives for the PHP Coding category.

Archives

July 2007
  • Categories