Showing posts with label php. Show all posts
Showing posts with label php. Show all posts

09 February 2017

Get ip address using PHP


(If you found this article useful then share with your friends.)

08 February 2017

Using JSON inside PHP



(If you found this article useful then share with your friends.)

07 February 2017

PHP $$variable Double dollar sign Meaning

php

(If you found this article useful then share with your friends.)

06 February 2017

Write HTML in PHP

php logo


(If you found this article useful then share with your friends.)

04 February 2017

List of PHP ODBC functions

php logo


(If you found this article useful then share with your friends.)

03 February 2017

Write PHP code inside HTML file

php logo
To write PHP inside HTML, you need to use the less than and greater than brackets along with question mark with the starting one written 'php'.

For Example:

<div>
<?php 
?>
</div>



To print something inside php, we can use the fuction named echo which is predefined in php for printing.

For Example: 

<div>
<?php  echo "Hello World";
?>
</div>


To use php variable value inside html tag: We have taken a variable with name 'year' and value is set as '2014'. And the value of the variable has been used inside input text box inside the 'value' attribute .
<?php $year = 2014 ; ?> 
Input text:<input type="text" value="<?php echo $year ; ?>"/>

You have to be very careful when using php inside html. With little mistake, you may not get result.

Note: Make sure the file extension ends with .php , as php is scripting language, so it needs a server to execute the php code. 

Learn:  How to write HTML inside php

(If you found this article useful then share with your friends.)

02 February 2017

Short Note on Types of Array in PHP

php


(If you found this article useful then share with your friends.)

01 February 2017

How to Replace space with Dash in PHP

php logo

(If you found this article useful then share with your friends.)

14 December 2013

php File Upload - Tutorial with Example

php logo
PHP: Hypertext Preprocessor,scripting language

PHP File Upload

Example HTML code for file upload:
Form_page.php
<html>
<body>

<form action="upload_file.php" method="post"
enctype="multipart/form-data">
<label for="file">Filename:</label>
<input type="file" name="file" id="file" /> 
<br />
<input type="submit" name="submit" value="Submit" />
</form>

</body>
</html>


upload_file.php [Simple Code]
<?php
if ($_FILES["file"]["error"] > 0)
  {
  echo "Error: " . $_FILES["file"]["error"] . "<br />";
  }
else
  {
  echo "Upload: " . $_FILES["file"]["name"] . "<br />";
  echo "Type: " . $_FILES["file"]["type"] . "<br />";
  echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
  echo "Stored in: " . $_FILES["file"]["tmp_name"];
  }
?>

Parameters of $_FILES
  • $_FILES["file"]["name"] - the name of the uploaded file
  • $_FILES["file"]["type"] - the type of the uploaded file
  • $_FILES["file"]["size"] - the size in bytes of the uploaded file
  • $_FILES["file"]["tmp_name"] - the name of the temporary copy of the file stored on the server
  • $_FILES["file"]["error"] - the error code resulting from the file upload

upload_file.php [Code with Restriction on Upload, store the file in php temp folder]
<?php
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/pjpeg"))
&& ($_FILES["file"]["size"] < 20000))
  {
  if ($_FILES["file"]["error"] > 0)
    {
    echo "Error: " . $_FILES["file"]["error"] . "<br />";
    }
  else
    {
    echo "Upload: " . $_FILES["file"]["name"] . "<br />";
    echo "Type: " . $_FILES["file"]["type"] . "<br />";
    echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
    echo "Stored in: " . $_FILES["file"]["tmp_name"];
    }
  }
else
  {
  echo "Invalid file";
  }
?>


upload_file.php [Code with Restriction on Upload, store the file in destination folder]
<?php
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/pjpeg"))
&& ($_FILES["file"]["size"] < 20000))
  {
  if ($_FILES["file"]["error"] > 0)
    {
    echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
    }
  else
    {
    echo "Upload: " . $_FILES["file"]["name"] . "<br />";
    echo "Type: " . $_FILES["file"]["type"] . "<br />";
    echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
    echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br />";

    if (file_exists("upload/" . $_FILES["file"]["name"]))
      {
      echo $_FILES["file"]["name"] . " already exists. ";
      }
    else
      {
      move_uploaded_file($_FILES["file"]["tmp_name"],
      "upload/" . $_FILES["file"]["name"]);
      echo "Stored in: " . "upload/" . $_FILES["file"]["name"];
      }
    }
  }
else
  {
  echo "Invalid file";
  }
?>

(If you found this article useful then share with your friends.)

11 December 2013

How to decode JSON using PHP5 for Facebook application

json facebook
The new Graph API helps to read and write data to Facebook. Every object in the social graph has a unique ID. You can fetch the data associated with an object by fetching https://graph.facebook.com/ID. For example, the official page for the Facebook Platform has id 19292868552, so you can fetch the object at https://graph.facebook.com/19292868552 All responses are JSON objects. Here is the code to decode JSON OBJECT / to get data from the graph api link . 

Code Sample : //FETCHING THE JSON FROM THE URL

$jsonurl = "https://graph.facebook.com/". $user_id; $json = file_get_contents($jsonurl,0,null,null); $json_output = json_decode($json);

// THIS PRINTS THE WHOLE STRING OF JSON

var_dump( json_decode( $json,true)) ;

// ITS SAME AS YOU ARE USING it as an object

$user_firstName = $json_output->first_name;

//PRINTS THE FIRST NAME OF THE LOGGED IN USER

echo $user_firstName ;

You can also directly use the graph api for showing the profile picture. For example :
<img src="https://graph.facebook.com/<?= $user ?>/picture"/>

Link :

  Graph API Facebook Documentation

(If you found this article useful then share with your friends.)

26 May 2012

How to stop this from showing in php5 ? :: Warning: Cannot modify header information - headers already sent by (output started at ::

PHP5 Problem:
"Warning: Cannot modify header information - headers already sent by (output started at "

This warning message can be corrected by deleting the empty spaces and lines
before and after the < ? php and ? > tags .

for example :
Error Format :


Corrected Format:

(If you found this article useful then share with your friends.)

17 November 2011

PHP MySQL data not showing from result after query

Nice and helpful post.

Output: Resource id #3

Solution: use mysql_fetch_row( $result ) in a while loop to get all result in an array..


(If you found this article useful then share with your friends.)

23 December 2010

How to use PHP bot to get Wikipedia definitions

php
This code helps to parse wikipedia definitions on given keyword.
Here is a code given that parse webpage using php with Curl extension.
A function is given which takes the keyword as input and outputs the array which holds the data.
$url : Holds the link to be parsed.

File name : wiki_parse.php
<?php
//FUNCTION THAT :PARAMETER - KEYWORD , AND RETURNS WIKI DEFINITION (IN ARRAY FORMAT)
function wikidefinition($s) {
//ENGLISH WIKI
    $url = "http://en.wikipedia.org/w/api.php?action=opensearch&search=".urlencode($s)."&format=xml&limit=1";
    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_HTTPGET, TRUE);
    curl_setopt($ch, CURLOPT_POST, FALSE);
    curl_setopt($ch, CURLOPT_HEADER, false);
    curl_setopt($ch, CURLOPT_NOBODY, FALSE);
    curl_setopt($ch, CURLOPT_VERBOSE, FALSE);
    curl_setopt($ch, CURLOPT_REFERER, "");
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
    curl_setopt($ch, CURLOPT_MAXREDIRS, 4);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
    curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 6.1; he; rv:1.9.2.8) Gecko/20100722 Firefox/3.6.8");
   
    $page = curl_exec($ch);
    $xml = simplexml_load_string($page);
    if((string)$xml->Section->Item->Description) {
        return array((string)$xml->Section->Item->Text,
                     (string)$xml->Section->Item->Description,
                     (string)$xml->Section->Item->Url);
    } else {
        return "";
    }
}
//END OF FUNCTION WIKIDEFINITIONS



//USE OF FUNCTION
$data = wikidefinition('Bangladesh') ;
//var_dump( wikidefinition('bangladesh') ) ; //displays the array content
echo "Word:"       . $data[0] . "<br/>";
echo "Definition:" . $data[1]  . "<br/>";
echo "Link:"       . $data[2] . "<br/>";

?>

Thanks to barattalo.it

(If you found this article useful then share with your friends.)

24 October 2010

Some useful PHP MYSQL functions

php
PHP MYSQL functions helps to connect to the database in MySQL software.
These are some of the most frequently used function names :


mysql_connect — Open a connection to a MySQL Server

mysql_create_db — Create a MySQL database

mysql_error — Returns the text of the error message from previous MySQL operation

mysql_fetch_array — Fetch a result row as an associative array, a numeric array, or both

mysql_num_rows — Get number of rows in result

mysql_query — Send a MySQL query

mysql_tablename — Get table name of field

mysql_close — Close MySQL connection

(If you found this article useful then share with your friends.)