Php Link won't execute

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
gilley55
Forum Newbie
Posts: 11
Joined: Tue Aug 19, 2008 10:13 am

Php Link won't execute

Post by gilley55 »

The problem is highlighted in red

after my if statement executes and the draw function is called, php doesn't execute the link

header('Location: http://www.example.com/');
am not sure why.
please help me and

thanks in advance.


.
<?php
session_start();
require_once 'Image/Barcode.php';


$file = 'afiles.txt' or die('Could not read file!');
// read file into array
$data = file($file) or die('Could not read file!');
// loop through array and print each line
$numLines=count($data);
natcasesort($data);

$colCtr = 0;
$cols = 8; # Number of columns to display
$items=$_GET['items'];
$items=40;
$imageCount=0;
$pos =0;
$pos=$_GET['pos'];

if(!$pos){
$pos= 0;
}


$position = $pos * $items;



$data = array_unique($data);
$data1 = array_slice($data, $position, $items);
$numLines=count($data1);

$index=1;
setcookie("index", $index, time()+3600);
$index=$_COOKIE["index"];

$temp = explode(",", $data1[$index]);
$images=trim($temp[0]);
$descriptions=trim($temp[1]);
$value=trim($temp[2]);


if(isValid($value)==true){

Image_Barcode::draw($value, 'upca', 'png');

header('Location: http://www.example.com/');
}

else
{
echo "NO UPC";

}


function isValid($value)
{
$valueString = (string) $value;
//$this->_setValue($valueString);

if ('' === $valueString) {
$error='STRING_EMPTY';
return false;
}

if (strlen($valueString) !== 12) {
$error='INVALID_LENGTH';

return false;
}

$barcode = substr($valueString, 0, -1);
$oddSum = 0;
$evenSum = 0;

for ($i = 0; $i < 11; $i++) {
if ($i % 2 === 0) {
$oddSum += $barcode[$i] * 3;
} elseif ($i % 2 === 1) {
$evenSum += $barcode[$i];
}
}

$calculation = ($oddSum + $evenSum) % 10;
$checksum = ($calculation === 0) ? 0 : 10-$calculation;

if ($valueString[11] != $checksum) {
$error='INVALID';
return false;
}

return true;
}
?>
User avatar
greyhoundcode
Forum Regular
Posts: 613
Joined: Mon Feb 11, 2008 4:22 am

Re: Php Link won't execute

Post by greyhoundcode »

It's good practice to follow a header("location:www...") with an exit() instruction. Also, you should look outside of your script - basically, a redirection using header("location:www...") will not work if anything at all has already been outputted.

So what I'm getting at is if your script comes after a bit of HTML, like so -

Code: Select all

<html>
<head>
 
<?php
    include "folder/myfunction.php";
?>
 
</head>
<body>
 
<p>Ipso lorem etc etc</p>
 
</body>
</html>
- then some items have already been output to the browser (starting with the opening <html> tag) and the redirection will not work. Make sure your script is executed first, and that nothing within the script generates output of any kind before your "if (condition) : redirect" statement.

Code: Select all

<?php
    include "folder/myfunction.php";
?>
 
<html>
<head>
 ...
User avatar
dude81
Forum Regular
Posts: 509
Joined: Mon Aug 29, 2005 6:26 am
Location: Pearls City

Re: Php Link won't execute

Post by dude81 »

Check what is happening to $value before the you pass to the isValid function, check what is happening to $value in isValid function. Use var_dump, as you are checking for data type of $value
User avatar
dude81
Forum Regular
Posts: 509
Joined: Mon Aug 29, 2005 6:26 am
Location: Pearls City

Re: Php Link won't execute

Post by dude81 »

greyhoundcode wrote:It's good practice to follow a header("location:www...") with an exit() instruction.
Follow the golden rule exit() after header, otherwise, below code also gets executed
gilley55
Forum Newbie
Posts: 11
Joined: Tue Aug 19, 2008 10:13 am

Re: Php Link won't execute

Post by gilley55 »

Ok i understand what you guys are telling me now thanks. Based on what you have told me i have concluded that becuase my bar code image was being outputted first .it prevented the redirect from working.

I would like to output the bar code image then leave the page. How can i do that ?

Thanks again
User avatar
dude81
Forum Regular
Posts: 509
Joined: Mon Aug 29, 2005 6:26 am
Location: Pearls City

Re: Php Link won't execute

Post by dude81 »

Code: Select all

 
print_r(Image_Barcode::draw($value, 'upca', 'png')); this should work
, If that does not work, do go to 'Image/Barcode.php' file do a debugging in draw method/function. This second method is not a good practice
gilley55
Forum Newbie
Posts: 11
Joined: Tue Aug 19, 2008 10:13 am

Re: Php Link won't execute

Post by gilley55 »

Thanks Dude81 for all the help

the following didn't work

print_r(Image_Barcode::draw($value, 'upca', 'png')); this should work

by the way the bar code image prints fine no problem is just the link that the page is suppose to be following after the bar code has finish output to the browser

here is the modifications i made with the advice i receive from this forum.

The Area in red is where the problem is.
Thanks again for your help.

<?php
session_start();
require_once 'Image/Barcode.php';

$file = 'afiles.txt' or die('Could not read file!');
// read file into array
$data = file($file) or die('Could not read file!');
// loop through array and print each line
$numLines=count($data);
natcasesort($data);

$colCtr = 0;
$cols = 8; # Number of columns to display
$items=$_GET['items'];
$items=40;
$imageCount=0;
$pos =0;
$pos=$_GET['pos'];

if(!$pos){
$pos= 0;
}


$position = $pos * $items;



$data = array_unique($data);
$data1 = array_slice($data, $position, $items);
$numLines=count($data1);

$index=1;
setcookie("index", $index, time()+3600);
$index=$_COOKIE["index"];

$temp = explode(",", $data1[$index]);
$images=trim($temp[0]);
$descriptions=trim($temp[1]);
$value=trim($temp[2]);


if(isValid($value)==true){
//Image_Barcode::draw($value, 'upca', 'png');

print_r(Image_Barcode::draw($value, 'upca', 'png')) //this should work

header("Location: http://www.google.com/") and exit();

}
else
{
echo "NO UPC";
}



function isValid($value)
{
$valueString = (string) $value;
//$this->_setValue($valueString);

if ('' === $valueString) {
$error='STRING_EMPTY';
return false;
}

if (strlen($valueString) !== 12) {
$error='INVALID_LENGTH';

return false;
}

$barcode = substr($valueString, 0, -1);
$oddSum = 0;
$evenSum = 0;

for ($i = 0; $i < 11; $i++) {
if ($i % 2 === 0) {
$oddSum += $barcode[$i] * 3;
} elseif ($i % 2 === 1) {
$evenSum += $barcode[$i];
}
}

$calculation = ($oddSum + $evenSum) % 10;
$checksum = ($calculation === 0) ? 0 : 10-$calculation;

if ($valueString[11] != $checksum) {
$error='INVALID';
return false;
}

return true;
}
?>
Post Reply