Why dosent this php function works ?

Hi!

Have this function:

function sjekk_om_flere_lokasjoner($lokasjon,$kundeid)
{
        $sql = "SELECT Lokasjons_id FROM Lokasjoner WHERE Lokasjons_id=$lokasjon and Kunde_id='$kundeid'";
        $res = mysql_query($sql);
        
        // IF THE QUERY SUCCEEDED
        if ($res)
        {
            // THERE SHOULD BE ONE ROW
            $num = mysql_num_rows($res);
            if ($num)
            {
                // Retuner 1 hvis finnes lokasjon
                return true;
          
            }
			else
			{
			    return false;
			}
        }
		else
		{
		  return false;
		}
}

Open in new window


I am using this function here:

<?php // RAY_EE_login.php
require_once('RAY_EE_config.php');
$table1 = 'Lokasjoner';
// ACCESS TO THIS PAGE IS TESTED BUT NOT CONTROLLED
if ($uid = access_control(TRUE))
{
   $brukerID = brukerident('xxx');
   $navn = $_GET['Navn'];
   $lokk = $_GET['lokk'];   
   $kundeid = kundeid();
   
   $finneslokasjon = sjekk_om_flere_lokasjoner($lokk,$kundeid);

   if ($finneslokasjon==false)
   {
        header("location: http://www.softkey.no/webtest/maiside.php");
	    exit;
   }
}
else
{
        header("location: http://www.softkey.no/webtest/xxx.php");
	    exit;
}
?>
<!DOCTYPE html>

<html>
<head>
<meta charset="utf-8" />
<title>Test system</title>

</head>

<body>
<link rel="stylesheet" href="default.css">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<table summary="Services, or Links box template" class="servicesT" cellspacing="1">
<tr><td colspan="2" class="header1" height="20"><a href="http://www.softkey.no/webtest/hovide.php" target="_self">
<img src="images/topp1.png" align="left" />
</a></td></tr>
<tr><td colspan="2" class="servHd" height="40"><?php echo utf8_encode($navn);?></td></tr>


<?php
$result3 = mysql_query("SELECT Lokasjonsnavn FROM {$table1} WHERE Lokasjons_id={$lokk} and Kunde_id='{$kundeid}'") or die(mysql_error());
while ($row3 = mysql_fetch_array($result3)){
    $navn = $row3['Lokasjonsnavn'];
?>
<tr>
	<td class="servBodL" ><?php echo utf8_encode($navn); ?></td>
</tr>
<?php
}

?>

</table>
<br>
<br><br>
<fieldset id="actions">
	    <br>
		<a href="http://www.softkey.no/webtest/gsistrerssqr.php?lokk=<?php echo $lokk; ?>&Kunde=<?php echo $kundeid; ?>&Navn=<?php echo  utf8_encode($navn); ?>&REGJN=1"><input type="submit" id="LOGIN" value="Scan"></a>
		
</fieldset>
</Form>

</body>
</html>

Open in new window


The problem is that the page return a blank page.

If i remove this code:
  $finneslokasjon = sjekk_om_flere_lokasjoner($lokk,$kundeid);

   if ($finneslokasjon==false)
   {
        header("location: http://www.softkey.no/webtest/maiside.php");
	    exit;
   }

Open in new window

its working fine ?

What is wrong with this code ?
LVL 2
team2005Asked:
Who is Participating?
 
Julian HansenCommented:
Change your two header() calls to die() instead

i.e.
header('location: xxx') => die('location: xxx');

Run again - and post back what message is displayed.
0
 
hieloCommented:
On line 4 of your first code segment, try:
$res = mysql_query($sql) or die(mysql_error() . ':<br />' . $sql);

Open in new window

If the query is failing, it should reveal why it is failing.


Also, on line 5 of your second code segment, you probably meant to have "==" (two equal signs):
if ($uid == access_control(TRUE)){...}

Open in new window


Lastly, change "location" to "Location"  (Upper Case "L"):
header("Location:...");
0
 
Kim WalkerWeb Programmer/TechnicianCommented:
0
Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

 
Julian HansenCommented:
@hielo,

Also, on line 5 of your second code segment, you probably meant to have "==" (two equal signs):
if ($uid == access_control(TRUE)){...}

Be careful here - it is possible this was supposed to be an assignment and test in one go ...

You can also refactor your function no need for all the else statements leading to a return false - make your function return false by default and return true only when conditions for doing so are met.

function sjekk_om_flere_lokasjoner($lokasjon,$kundeid)
{
  $num = 0;
  $sql = "SELECT Lokasjons_id FROM Lokasjoner WHERE Lokasjons_id=$lokasjon and Kunde_id='$kundeid'";
    $res = mysql_query($sql);
        
    // IF THE QUERY SUCCEEDED
    if ($res) {
      // THERE SHOULD BE ONE ROW
      $num = mysql_num_rows($res);
    }
// For debugging purposes
   else {
      echo "Error: " . mysql_error();
   }
    return ($num > 0);
}

Open in new window

NB: the mysql lib is deprecated as of php 5.5 and is going to dissappear in future releases. Might be a good time to switch to PDO / mysqli.
0
 
team2005Author Commented:
Hi!

Still the same problem -> blank page
and no php error message ?
0
 
Kim WalkerWeb Programmer/TechnicianCommented:
Do you have display errors turned off? This should turn them on if placed at the very beginning of your script:
ini_set('display_errors', '1');
error_reporting(E_ALL);

Open in new window

0
 
team2005Author Commented:
Hi!

Get this error messages:

Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at /home/2/s/softkey/www/webtest/side3.php:1) in /home/2/s/softkey/www/webtest/RAY_EE_config.php on line 13

Warning: Cannot modify header information - headers already sent by (output started at /home/2/s/softkey/www/webtest/side3.php:1) in /home/2/s/softkey/www/webtest/side3.php on line 18
0
 
Julian HansenCommented:
That's something else.

Not enough info on how your code is structured to continue - what is side3.php ?
0
 
team2005Author Commented:
Hi!

Here is side3.php

<?php // RAY_EE_login.php
require_once('RAY_EE_config.php');
$table1 = 'Lokasjoner';
// ACCESS TO THIS PAGE IS TESTED BUT NOT CONTROLLED
if ($uid = access_control(TRUE))
{
   $brukerID = brukerident('xxx');
   $navn = $_GET['Navn'];
   $lokk = $_GET['lokk'];   
   $kundeid = kundeid();
   
   $finneslokasjon = sjekk_om_flere_lokasjoner($lokk,$kundeid);

   if ($finneslokasjon==false)
   {
        header("location: http://www.softkey.no/webtest/hovedside.php");
	    exit;
   }
}
else
{
        header("location: http://www.softkey.no/webtest/login.php");
	    exit;
}
?>
<!DOCTYPE html>

<html>
<head>
<meta charset="utf-8" />
<title>Test system</title>

</head>

<body>
<link rel="stylesheet" href="default.css">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<table summary="Services, or Links box template" class="servicesT" cellspacing="1">
<tr><td colspan="2" class="header1" height="20"><a href="http://www.softkey.no/webtest/hovedside.php" target="_self">
<img src="images/topp1.png" align="left" />
</a></td></tr>
<tr><td colspan="2" class="servHd" height="40"><?php echo $navn;?></td></tr>


<?php
$result3 = mysql_query("SELECT Lokasjonsnavn FROM {$table1} WHERE Lokasjons_id={$lokk} and Kunde_id='{$kundeid}'") or die(mysql_error());
while ($row3 = mysql_fetch_array($result3)){
    $navn = utf8_encode($row3['Lokasjonsnavn']);
?>
<tr>
	<td class="servBodL" ><?php echo $navn; ?></td>
</tr>
<?php
}

?>

</table>
<br>
<br><br>
<fieldset id="actions">
	    <br>
		<a href="http://www.softkey.no/webtest/registrerqr.php?lokk=<?php echo $lokk; ?>&Kunde=<?php echo $kundeid; ?>&Navn=<?php echo $navn; ?>&REGJN=1"><input type="submit" id="LOGIN" value="Scan"></a>
		
</fieldset>
</Form>

</body>
</html>

Open in new window

0
 
Kim WalkerWeb Programmer/TechnicianCommented:
Were the errors you posted above generated after you added the two lines for error display I recommended? I don't see them in side3.php and that would throw off the line numbering.

The issue is obviously that output is being generated before the session start command and before the header is issued. I must assume that a session is being started in the included "RAY_EE_config.php" file. The question is where and how is output being generated before that unless another error was generated before these two.

Be sure to post the ENTIRE list of errors and the latest version of side3.php.
0
 
Julian HansenCommented:
@team2005 - in order for us to help you we need information. The code you have posted does not appear to contain any of the changes recommended in this thread. We also appear to be dealing with moving targets - the original post was about a blank page now it is about warnings relating to headers being sent after output is sent to the browser.

We need to see what changed to create the second issue - which is not necessarily linked to the first.

Post the code that caused the error along with any supporting files (as attachments).

When attempting recommendations made in this thread feedback specifically to the person who gave you the recommendation so we know what post you are responding to otherwise it gets extremely confusing.
0
 
team2005Author Commented:
Thanks
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.