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;
}
?>
Php Link won't execute
Moderator: General Moderators
- greyhoundcode
- Forum Regular
- Posts: 613
- Joined: Mon Feb 11, 2008 4:22 am
Re: Php Link won't execute
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 -
- 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.
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>Code: Select all
<?php
include "folder/myfunction.php";
?>
<html>
<head>
...Re: Php Link won't execute
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
Re: Php Link won't execute
Follow the golden rule exit() after header, otherwise, below code also gets executedgreyhoundcode wrote:It's good practice to follow a header("location:www...") with an exit() instruction.
Re: Php Link won't execute
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
I would like to output the bar code image then leave the page. How can i do that ?
Thanks again
Re: Php Link won't execute
Code: Select all
print_r(Image_Barcode::draw($value, 'upca', 'png')); this should workRe: Php Link won't execute
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;
}
?>
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;
}
?>