Solved

Why dosent this php function works ?

Posted on 2013-06-17
12
200 Views
Last Modified: 2013-06-20
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 ?
0
Comment
Question by:team2005
  • 4
  • 4
  • 3
  • +1
12 Comments
 
LVL 82

Expert Comment

by:hielo
ID: 39253129
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
 
LVL 22

Expert Comment

by:Kim Walker
ID: 39253161
0
 
LVL 53

Expert Comment

by:Julian Hansen
ID: 39253194
@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
 
LVL 2

Author Comment

by:team2005
ID: 39253307
Hi!

Still the same problem -> blank page
and no php error message ?
0
 
LVL 53

Accepted Solution

by:
Julian Hansen earned 250 total points
ID: 39253521
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
 
LVL 22

Assisted Solution

by:Kim Walker
Kim Walker earned 250 total points
ID: 39253582
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 2

Author Comment

by:team2005
ID: 39253804
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
 
LVL 53

Expert Comment

by:Julian Hansen
ID: 39254109
That's something else.

Not enough info on how your code is structured to continue - what is side3.php ?
0
 
LVL 2

Author Comment

by:team2005
ID: 39254243
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
 
LVL 22

Expert Comment

by:Kim Walker
ID: 39254283
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
 
LVL 53

Expert Comment

by:Julian Hansen
ID: 39255336
@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
 
LVL 2

Author Closing Comment

by:team2005
ID: 39261953
Thanks
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This article describes how to create custom column layout styles for Bootstrap. The article uses 5 columns to illustrate the concept, but the principle can be extended to any number of columns.
Is your Office 365 signature not working the way you want it to? Are signature updates taking up too much of your time? Let's run through the most common problems that an IT administrator can encounter when dealing with Office 365 email signatures.
The viewer will receive an overview of the basics of CSS showing inline styles. In the head tags set up your style tags: (CODE) Reference the nav tag and set your properties.: (CODE) Set the reference for the UL element and styles for it to ensu…
The viewer will learn the benefit of using external CSS files and the relationship between class and ID selectors. Create your external css file by saving it as style.css then set up your style tags: (CODE) Reference the nav tag and set your prop…

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

29 Experts available now in Live!

Get 1:1 Help Now