Solved

Why dosent this php function works ?

Posted on 2013-06-17
12
197 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 21

Expert Comment

by:Kim Walker
ID: 39253161
0
 
LVL 51

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 51

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 21

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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
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 51

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 21

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 51

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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
Use these top 10 tips to master the art of email signature design. Create an email signature design that will easily wow recipients, promote your brand and highlight your professionalism.
In this tutorial viewers will learn how add a scalable full-width header using CSS3. Create a new HTML document with an internal stylesheet. Set a tiled background.:  Create a new div and name it Header. Position it with position:absolute at the top…
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

760 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

18 Experts available now in Live!

Get 1:1 Help Now