You are not logged in.
Hi,
I'm an amateur in writing code and have learned to write simple dynamic websites with it. My current project is to take my codebase I've rearranged throughout several websites in several years and make a 'file management'-program to use at my office. So far I have simple file management (create, search) and simple integration with our caldav-server and asterisk-server. Next up is writing to caldav and making a GUI to edit files and caldav items. Further along I'll be integrating our LDAP-addressbook (read/write) and the possibility to use all the information in openoffice templates.
So far my aspirations.
I'm struggling with a fundamental problem that has risen somewhere last week without me noticing it, so I cannot trace back the culprit and comparing to older code on the same base doesn't help me. I
The code (simplified) of my process-class, where all forms come together and where input data is transferred into usable database-queries. The three 'worker-functions' are called for and work perfectly, but they should end with the header-directive at the end of each function. That is not the case: after completing the function (any one of them), the header-directive in bold (inside the Process-function) is executed. So I guess the three worker-functions don't end with their own header-directive and just continue with the Process-function.
The code:
<?php
class Process
{
/* Class constructor */
function Process(){
if(isset($_POST['dossierNieuw'])){
$this->dosNieuw();
}
if(isset($_POST['calZoek'])){
$this->calZoek();
}
if(isset($_POST['dosZoek'])){
$this->dosZoek();
}
else if($session->logged_in){
$this->procLogout();
}
/**
* Should not get here, which means user is viewing this page
* by mistake and therefore is redirected.
*/
[b]else {
header("Location: index.php");
}[/b]
}
function dosNieuw(){
[...]
header("Location: index.php?p=dos&s=view&w=$year");
}
function calZoek(){
[...]
header("Location: index.php?p=cal&s=search&w=$search");
}
function dosZoek(){
[...]
header("Location: index.php?p=dos&s=view&w=$search");
}
};
/* Initialize process */
$process = new Process;
?>
It feels like I'm overlooking something fundamental/simple here. I guess that's what you get when you are thrown in a scripting language without a decent basic knowledge
Thx in advance for any hints/tips...
Offline
BTW: adding
die();
before the header-directive inside the Process-function solves the problem, but that doesn't make it right...
Offline
I seem to have found the problem: instead of IF - IF - IF - ELSE IF - ELSE if made it into IF - ELSE IF - ELSE IF - ELSE IF - ELSE.
I guess this is 'more correct', since neither of those functions would be called at the same time, but I fail to see why the final ELSE was called after every IF
Oh well,
it works.
Offline
HTTP redirect (301) headers should be sent only once and no output is allowed after it. Your code can potentially send up to 3 redirects. If you set a 'dossierNieuw' post parameter, you send a redirect. Then if there's also a 'calZoek' parameter, you send another one. Finally you send another redirect no matter what. That's why your second if/elseif approach works: you don't allow the program to process the rest of the if conditions once you find a condition that is true. If you end up using multiple if/else if conditions then it's probably better to use switch/case instead.
Using switch/case you'd have the following code:
switch (true) {
case isset($_POST['dossierNieuw']):
dosNieuw();
break;
case isset($_POST['calZoek']):
calZoek();
break;
case isset($_POST['dosZoek']):
dosZoek;
break;
case $session->logged_in:
procLogout();
break;
default:
header("Location: index.php");
}
PS. You don't have to use a class for that, unless I'm missing something. You're not using any OO features, if you're just calling a few functions statically. You can just declare those functions outside a class and still get it to work.
Last edited by Teoulas (2009-09-25 17:34:49)
Offline