Category: PHP

Enabling remember me on signin page using php cookies

In this application, we use PHP cookes to enable “remember me” option <?php session_start(); ?> <html> <head> <?php //print_r($_COOKIE); /* if(strlen($_COOKIE['userid'])!=0) { $_SESSION['isloggedin'] = true; echo "<script language='javascript'>window.location.href='default.php';</script>"; } */ $userid = isset($_COOKIE['userid'])?$_COOKIE['userid']:""; $passwd = isset($_COOKIE['passwd'])?$_COOKIE['passwd']:""; if (isset($_POST['btnsubmit']) ) { //setcookie("count",500); $username = $_POST['txtname']; $password = $_POST['txtpassword']; $remember = isset($_POST['chkremember'])?$_POST['chkremember']:""; if($username == "ali" && $password

arrays in php

In this example, arrays are declared in PHP using name/value pairs and without name value pairs <html> <head> </head> <body> <?php $array = array( "KHI" => "Karachi", "LHR" => "Lahore", "ISL" => "Islamabad"); $array1 = array("pakistan", "usa", "england"); //var_dump($array); //print_r($array); foreach($array as $key => $value) { echo $key . " " . $value . "<br>";

A simple class in PHP

In this example, a simple class is made in PHP. The class object is created, using which various functions of the class are called <html> <head> </head> <body> <?php class student { var $foo; var $bar; var $ans; function student() { $this -> foo = "foo"; $this -> bar = "bar1"; } function display() {

How to prevent SQL injection attacks in PHP

To prevent SQL injection attacks or javascript attacks in your PHP web application, always use parameterized query with prepared statements like: $txtuserid = htmlspecialchars(addslashes(trim($_POST['txtid']))); $query = "select * from users where userid=:userid" try { $stmt = $conn->prepare($query); $stmt->bindParam(':userid', $txtuserid); //$txtuserid getting value through $_POST or URL string $stmt->execute(); echo "Record inserted successfully..."; } catch(PDOException $e)