rss search

next page next page close

How to download image to your server using PHP?

How to download image to your server using PHP?

I had been google-ing over this for a while, and couldn’t find the answer. Finally with some luck I found a way to download an image, and put it on your server. The code will look like this:

$imageUrl = "http://graph.facebook.com/" . $facebook . "/picture?type=large";
$filename = realpath(dirname(__FILE__))."/foo/foo.png";
$handle = file_get_contents($imageUrl);
file_put_contents($filename, $handle);

next page next page close

How to Check Cookie using PHP?

How to Check Cookie using PHP?

If you use JavaScript, it won’t work on the Internet Explorer because IE got a bug.

      setcookie ('test', 'test', time() + 60);
      if (!isset($test) && !isset($_COOKIE['test'])
          && !isset($HTTP_COOKIE_VARS['test'])) {
          echo "Cookie is not working";
      } else {
          echo "Cookie is working";
     }

next page next page close

PHP Session Tutorial

PHP Session Tutorial

Part 1 – Starting a Session

A session is a way to store information (in the form of variables) to be used across multiple pages. Unlike a cookie, specific variable information is not stored on the users computer. It is also unlike other variables in the sense that we are not passing them individually to each new page, but instead retrieving them from the session we open at beginning of each page.

Call this code mypage.php

    // this starts the session
    session_start();

    // this sets variables in the session
    $_SESSION['color']='red';
    $_SESSION['size']='small';
    $_SESSION['shape']='round';
    print "Done";

The first thing we do with this code, is open the session using session_start(). We then set our first session variables (color, size and shape) to be red, small and round respectively.

Just like with our cookies, the

session_start()

code must be in the header and you can not send anything to the browser before it. It’s best to just put it directly after the to avoid potential problems.

So how will it know it's me? Most sessions set a cookie on your computer to uses as a key... it will look something like this: 350401be75bbb0fafd3d912a1a1d5e54. Then when a session is opened on another page, it scans your computer for a key. If there is a match, it accesses that session, if not it starts a new session for you.

Part 2 - Using Session Variables

Now we are going to make a second page. We again will start with session_start() (we need this on every page) - and we will access the session information we set on our first page. Notice we aren't passing any variables, they are all stored in the session.

Call this code mypage2.php

    // this starts the session
    session_start();

    // echo variable from the session, we set this on our other page
    echo "Our color value is ".$_SESSION['color'];
    echo "Our size value is ".$_SESSION['size'];
    echo "Our shape value is ".$_SESSION['shape'];

All of the values are stored in the $_SESSION array, which we access here. Another way to show this is to simply run this code:

    session_start();
    Print_r ($_SESSION);

You can also store an array within the session array. Let's go back to our mypage.php file and edit it slightly to do this:

    session_start();

    // makes an array
    $colors=array('red', 'yellow', 'blue');
    // adds it to our session
    $_SESSION['color']=$colors;
    $_SESSION['size']='small';
    $_SESSION['shape']='round';
    print "Done";

Now let's run this on mypage2.php to show our new information:

    session_start();
    Print_r ($_SESSION);
    echo "

";

    //echo a single entry from the array
    echo $_SESSION['color'][2];

Part 3 - Modify or Remove Session

    // you have to open the session to be able to modify or remove it
    session_start();

    // to change a variable, just overwrite it
    $_SESSION['size']='large';

    //you can remove a single variable in the session
    unset($_SESSION['shape']);

    // or this would remove all the variables in the session, but not the session itself
    session_unset();

    // this would destroy the session variables
    session_destroy();

The code above demonstrates how to edit or remove individual session variables, or the entire session. To change a session variable we just reset it to something else. We can use unset() to remove a single variable, or session_unset() to remove all variables for a session. We can also use session_destroy() to destroy the session completely.

By default a session lasts until the user closes their browser. This can be changed in the php.ini file by change the 0 in session.cookie_lifetime = 0 to be the number of seconds you want the session to last, or by using session_set_cookie_params().

About.com

next page next page close

Member Area PHP Script

Member Area PHP Script
// Connects to your Database
mysql_connect("your.hostaddress.com", "username", "password") or die(mysql_error());
mysql_select_db("Database_Name") or die(mysql_error());

//checks cookies to make sure they are logged in
if(isset($_COOKIE['ID_my_site']))
{
$username = $_COOKIE['ID_my_site'];
$pass = $_COOKIE['Key_my_site'];
$check = mysql_query("SELECT * FROM users WHERE username = '$username'")or die(mysql_error());
while($info = mysql_fetch_array( $check ))
{

//if the cookie has the wrong password, they are taken to the login page
if ($pass != $info['password'])
{ header("Location: login.php");
}

//otherwise they are shown the admin area
else
{
echo "Admin Area

";
echo "Your Content

";
echo "Logout";
}
}
}
else

//if the cookie does not exist, they are taken to the login screen
{
header("Location: login.php");
}

This code checks the cookies to make sure the user is logged in, the same way the login page did. If they are logged in, they are shown the members area. If they are not logged in they are redirected to the login page.

next page next page close

PHP Inheritance

PHP Inheritance
interface hasArea {
	public function getArea();
}

class Rectangle implements hasArea {
	static $w;
	static $l;
	public function __construct($w, $l) {
		$this->w = $w;
		$this->l = $l;
	}

	public function getArea() {
		return $this->w * $this->l;
	}
}

class Square extends Rectangle {
	public function setWidth($w) {
		parent::$w = $w;
	}

	public function setLength($l) {
		parent::$l = $l;
	}

	public function getArea() {
		return parent::$w * parent::$l;
	}

	public function getArea2() {
		return $this->w * $this->l;
	}
}

$rect = new Rectangle(10, 5);
print $rect->getArea();
print "";
$sqr = new Square(5, 5);
print $sqr->getArea();
print "";
print $sqr->getArea2();
print "";
$sqr->setWidth(10);
$sqr->setLength(10);
print $sqr->getArea();

/*
	Output:
	50
	0
	25
	100
*/

PHP supports inheritance and polymorphism. The above is one tiny example when I played around with it. If you see the output, you realize that setWidth and setLength would set the parent variable, in which is NOT set even I’ve instantiated Rectangle using $w and $l parameter on its constructor.

next page

How to download image to your server using PHP?

I had been google-ing over this for a while, and couldn’t find the answer. Finally...
article post

How to Check Cookie using PHP?

If you use JavaScript, it won’t work on the Internet Explorer because IE got a...
article post

PHP Session Tutorial

Part 1 – Starting a Session A session is a way to store information (in the form of...
article post

Member Area PHP Script

// Connects to your Database mysql_connect("your.hostaddress.com", "username",...
article post

PHP Inheritance

interface hasArea { public function getArea(); } class Rectangle implements hasArea...
article post