Troubleshooting PHP Warning: Cannot modify header information – headers already sent

Code, Hack on May 15th, 2012 No Comments

Add the following code to the top of your page:

 PHP |  copy code |? 
1
<?php ob_start(); ?>

And add this code to the bottom of your page:

 PHP |  copy code |? 
1
<?php ob_flush(); ?>

Tags: , , , , , , ,

How to Query a Database or String for Specific Word(s)

Code, Hack on April 30th, 2012 Comments Off

Query from database

 PHP |  copy code |? 
1
<?php $query = 'SELECT * FROM table WHERE field LIKE %variable%' ?>

Query from string

 PHP |  copy code |? 
1
2
<?php
3
$statement = 'Hello, how are you doing today?';
4
//Checks string for the word "are"
5
$query = 'if (strpos($statement,'are') !== false) {
6
    echo 'true';
7
}
8
?>

Tags: , , , ,

Export MySQL to Excel Using PHP

Code, Hack on February 22nd, 2012 Comments Off

The code below will export every column name and value from your database into an excel document.

Note: Delete the line spaces if you receive a parse error.

 PHP |  copy code |? 
01
<?php
02
/*******EDIT LINES 3-8*******/
03
$DB_Server = "localhost"; //MySQL Server 
04
$DB_Username = "username"; //MySQL Username 
05
$DB_Password = "password";             //MySQL Password 
06
$DB_DBName = "databasename";         //MySQL Database Name 
07
$DB_TBLName = "tablename"; //MySQL Table Name
08
$filename = "excelfilename";         //File Name
09
/*******YOU DO NOT NEED TO EDIT ANYTHING BELOW THIS LINE*******/
10
//create MySQL connection
11
$sql = "Select * from $DB_TBLName";
12
$Connect = @mysql_connect($DB_Server, $DB_Username, $DB_Password)
13
    or die("Couldn't connect to MySQL:<br>" . mysql_error() . "<br>" . mysql_errno());
14
//select database
15
$Db = @mysql_select_db($DB_DBName, $Connect)
16
    or die("Couldn't select database:<br>" . mysql_error(). "<br>" . mysql_errno());
17
//execute query
18
$result = @mysql_query($sql,$Connect)
19
    or die("Couldn't execute query:<br>" . mysql_error(). "<br>" . mysql_errno());
20
$file_ending = "xls";
21
 
22
//header info for browser
23
header("Content-Type: application/xls");
24
header("Content-Disposition: attachment; filename=$filename.xls");
25
header("Pragma: no-cache");
26
header("Expires: 0");
27
 
28
/*******Start of Formatting for Excel*******/
29
//define separator (defines columns in excel & tabs in word)
30
$sep = "\t"; //tabbed character
31
 
32
//start of printing column names as names of MySQL fields
33
for ($i = 0; $i < mysql_num_fields($result); $i++) {
34
echo mysql_field_name($result,$i) . "\t";
35
}
36
print("\n");
37
//end of printing column names
38
 
39
//start while loop to get data
40
    while($row = mysql_fetch_row($result))
41
    {
42
        $schema_insert = "";
43
        for($j=0; $j<mysql_num_fields($result);$j++)
44
        {
45
            if(!isset($row[$j]))
46
                $schema_insert .= "NULL".$sep;
47
            elseif ($row[$j] != "")
48
                $schema_insert .= "$row[$j]".$sep;
49
            else
50
                $schema_insert .= "".$sep;
51
        }
52
        $schema_insert = str_replace($sep."$", "", $schema_insert);
53
 $schema_insert = preg_replace("/\r\n|\n\r|\n|\r/", " ", $schema_insert);
54
        $schema_insert .= "\t";
55
        print(trim($schema_insert));
56
        print "\n";
57
    }
58
?>
59

Tags: , , ,

PCI Compliance – Simplified

Information on November 30th, 2011 No Comments

When making a payment online there is a merchant account and a gateway account. The merchant account is the bank account (where the money is deposited) and the gateway is the payment processor. So when you go to a website and fill in the information (name, address, credit card #, etc.) the gateway is verifying this information prior to depositing it into the merchant account. The Payment Card Industry (PCI) is an organization that requires certain measures to be met as far as security goes for the gateway account. The gateway can be fined or shut down by PCI for not being compliant with their standards, so the gateways contract out these third-party companies to ensure that all of their customers are compliant. If the company is not compliant, the gateway will add a fee to the account because they are taking a risk.

Are you able to choose who your PCI compliance is verify through?: Yes and No. It depends what gateway account the company is using. All of these PCI standards are based on the volume of transactions. For companies that have a lot of transactions, there is a higher level of compliance that is required. A large (gateway) company they will typically contract out a company to handle these test and you need to verify compliance through them. The smaller companies (some banks provide gateways) only require a self-assessment which can be completed in about 5 minutes online. This assessment is also contracted out to third-party companies.

Once your client has become PCI compliant, they will no longer be charged the extra fee. The PCI compliance should also be built into the gateway fees, there should not be an extra charge to your client for this service.

Tags: , ,

Find and Replace in SQL

Hack on October 20th, 2011 No Comments

This is a very useful code for making edits on the fly, especially if you have extra spacing or quotes after importing values into your database.

 SQL |  copy code |? 
1
UPDATE tablename SET tablefield = REPLACE(tablefield,'oldstring','newstring');

Tags: , ,

Block IP Range using Wildcard with PHP

Code, Hack on October 17th, 2011 No Comments

 PHP |  copy code |? 
01
// array's of banned IP addresses
02
$bannedIP = array("^10.999.00.*", "^99.88.77.*", "192.168.0.1", "^66.77.*.*");
03
if(in_array($_SERVER['REMOTE_ADDR'],$bannedIP)) {
04
     // this is for exact matches of IP address in array
05
     header("Location: http://www.domain.com");
06
     exit();
07
} else {
08
     // this is for wild card matches
09
     foreach($bannedIP as $ip) {
10
          if(eregi($ip,$_SERVER['REMOTE_ADDR'])) {
11
               header("Location: http://www.domain.com");
12
               exit();
13
          }
14
     }
15
}

Tags: , , ,

Obtain a Users IP Address with PHP

Hack on October 17th, 2011 No Comments

 PHP |  copy code |? 
1
if ( isset($_SERVER["REMOTE_ADDR"]) )    { 
2
 $userip = $_SERVER["REMOTE_ADDR"]; 
3
 } else if ( isset($_SERVER["HTTP_X_FORWARDED_FOR!"]) )    { 
4
 $userip = $_SERVER["HTTP_X_FORWARDED_FOR"]; 
5
 } else if ( isset($_SERVER["HTTP_CLIENT_IP"]) )    { 
6
 $userip = $_SERVER["HTTP_CLIENT_IP"]; 
7
 } 
8
 
9
 echo 'IP: '.$userip;

Tags: , ,

Update Checkbox Value Using PHP & MySQL

Hack on September 27th, 2011 No Comments

This code will send exactly two query’s to update a INT (0 or 1) checkbox field in a MySQL database.

The IN operational statement let’s you match on a list of values in a clause. The implode statement will turn the array (shown below) into a string.

 PHP |  copy code |? 
01
02
<?php
03
	if (isset($_POST['doSave'])){
04
 		if (isset($_POST['checkbox'])){
05
			$doSave = implode(',',$_POST['checkbox']);
06
			//if checkbox IS checked, update the database with a value of 1
07
			 $updatequery = "update databasename set checkbox='1' where id IN ($doSave)";
08
			 mysql_query($updatequery) or die(myql_error());
09
			//if checkbox IS NOT checked, update the database with a value of 0
10
			 $updatequery2 = "update databasename set checkbox='0' where id NOT IN ($doSave)";
11
			 mysql_query($updatequery2) or die(myql_error());}
12
		if (!isset($_POST['checkbox'])){
13
			//if checkbox IS NOT set, update the database with a value of 0
14
			 $doSave = implode(',',$_POST['checkbox']);
15
			 $updatequery2 = "update databasename set checkbox='0' where id NOT IN ($doSave)";
16
			 mysql_query($updatequery2) or die(myql_error());}
17
	}
18
?>
19

 HTML |  copy code |? 
1
<form name="formname" action="" method="post">

The checkboxes are populated through a while loop (not shown). Each checkbox has the same name, but a unique value. The $id variable is the unique field that is populated from the database. Include brackets [] in order to turn the field into an array.
 PHP |  copy code |? 
1
if($database['checkbox'] == 1) { $set_checked = "CHECKED";}
2
  else{$set_checked = ""; }
3
  print "<input type=\"checkbox\" name=\"checkbox[]\" value=\"$id\" $set_checked />";

 HTML |  copy code |? 
1
<input name="doSave" type="submit" id="doSave" value="Save"></form>

Tags: , ,

Redirect No Host to WWW Host

Hack on September 23rd, 2011 No Comments

Here is the code required to redirect no host to a www host. PHP and ASP versions of the code are included.

PHP

 PHP |  copy code |? 
1
<?php
2
if (substr($_SERVER['HTTP_HOST'],0,3) != 'www') {
3
 header('HTTP/1.1 301 Moved Permanently'); 
4
 header('Location: http://www.'.$_SERVER['HTTP_HOST']
5
 .$_SERVER['REQUEST_URI']);
6
}
7
?>

ASP
 ASP |  copy code |? 
1
<% if Request.ServerVariables("HTTP_HOST") = "domain.com" then Response.Redirect("http://www.domain.com") %>

Tags: , , , , ,

NAMIC Web Services

Featured on August 25th, 2011 No Comments

NAMIC Web Services (NWS), a division of NAMIC Service Corporation, is a professional web design company created to offer technology solutions to NAMIC Members and other industry organizations. NAMIC is the nation’s largest P/C insurance industry trade association, with more than 1,400 members.