1

I have a small time slot booking system, where I can click a link called: Reserve, and then I reserve that given time.

However, the page doesn't refresh after I've clicked on reserve. Therefore it's possible for a user to click the same reserve link twice. Whitch they shouldn't be able to.

if (isset ( $_GET ['reserved'] )) { 
    $sqlreserve = "INSERT INTO calendar (eventDate, timeslot) VALUES ('" . $dateToCompare . "','" . intval($_GET['t']) . "');";
    $resultreserve = mysqli_query ( $mysqli1, $sqlreserve );
    if ($resultreserve) {
        header('Location: '.$_SERVER['PHP_SELF']);
    } else {
        echo "Event Failed to add";
    }
}

If my insert works, then I call: header('Location: '.$_SERVER['PHP_SELF']);

I'm working on localhost, if that has anything to say?

EDIT:

The way I create my links and the text saying that a slot is booked is like this:

    if (mysqli_num_rows ( $result ) == 0) {
        echo "<a href='" . $_SERVER ['PHP_SELF'] . "?month=" . $month . "&day=" . $day . "&year=" . $year . "&t={$time}&v=true&f=true&reserved=true'><h3 style='color: rgb(255,0,0);'>Reserve</h3></a>";
    } else {
        echo "<h3>Not Available, taken by:</h3>";
        while ( $row = mysqli_fetch_array ( $result ) ) {
            echo "<br />";
        }

}

EDIT. My Error:

Cannot modify header information - headers already sent by (output started.....)

for($i = 1; $i < $numDays; $i ++, $counter ++) {
                $timeStamp = strtotime ( "$year-$month-$i" );
                if ($i == 1) {
                    $firstDay = date ( "w", $timeStamp );
                    for($j = 0; $j < $firstDay; $j ++, $counter ++) {
                        echo "<td>&nbsp;</td>";
                    }
                }
                if ($counter % 7 == 0) {
                    echo "</tr><tr>";
                }
                $monthstring = $month;
                $monthlength = strlen ( $monthstring );
                $daystring = $i;
                $daylength = strlen ( $daystring );
                if ($monthlength <= 1) {
                    $monthstring = "0" . $monthstring;
                }
                if ($daylength <= 1) {
                    $daystring = "0" . $daystring;
                }

                $todaysDate = date ( "m/d/Y" );
                $dateToCompare = $monthstring . '/' . $daystring . '/' . $year;
                echo "<td align='center' ";
                if ($todaysDate == $dateToCompare) {
                    echo "class='today'";
                } else {
                    $sqlCount = "SELECT * FROM calendar WHERE eventDate='" . $dateToCompare . "'";
                    $noOfEvent = mysqli_num_rows ( mysqli_query ( $mysqli1, $sqlCount ) );
                    if ($noOfEvent >= 1) {
                        echo "class='event'";
                    }
                }
                echo "><a href='" . $_SERVER ['PHP_SELF'] . "?month=" . $monthstring . "&day=" . $daystring . "&year=" . $year . "&v=true'>" . $i . "</a></td>";
            }

The line affected is:

echo "><a href='" . $_SERVER ['PHP_SELF'] . "?month=" . $monthstring . "&day=" . $daystring . "&year=" . $year . "&v=true'>" . $i . "</a></td>";

It is in another file where I have my calendar, in which I have links to the specific day that I wan't to book the timeslots fore:

enter image description here

2 Answers2

5

Try this..

header('Location: '.$_SERVER['PHP_SELF']);
exit;

I think your code is continuing...

A more in-depth explanation can be found here: Why I have to call 'exit' after redirection through header('Location..') in PHP?

EDIT

It's clear now that Milen Georgiev answer is correct. You output content to the browser before you reach the header(); You need to move the if statement in the top part of your PHP code to avoid the header content error.

if (isset ( $_GET ['reserved'] )) { 
    $sqlreserve = "INSERT INTO calendar (eventDate, timeslot) VALUES ('" . $dateToCompare . "','" . intval($_GET['t']) . "');";
    $resultreserve = mysqli_query ( $mysqli1, $sqlreserve );
    if ($resultreserve) {
        header('Location: '.$_SERVER['PHP_SELF']);
        exit;
    } else {
        echo "Event Failed to add";
    }
}
Community
  • 1
  • 1
Ronnie Jespersen
  • 950
  • 2
  • 9
  • 22
  • Thank you for the answer, I tried it, but it didn't work. I've made an eddit to my question, including more of my code. –  Apr 15 '14 at 11:12
  • Okay two things then... please still use exit; after the header... it's best pratice since you dont want the rest of the code to execute. Secondly can you please try and do a echo "hello"; inside the if ($resultreserve) { statement... just to make sure you actually enter the statement. – Ronnie Jespersen Apr 15 '14 at 11:15
  • Ah. I have an error now: Cannot modify header information - headers already sent by (output started.....) I've added the affected area to my question. –  Apr 15 '14 at 11:22
  • Just to make clear... was the error there before or after the echo? That type of error occurs when you output something on the secreen before a header(); If there is no error when removing the echo "hello"; before the header(); then its not that error that's effecting you... if the error is there when you remove the echo then @Milen Georgiev was right... – Ronnie Jespersen Apr 15 '14 at 11:26
  • The error is still there after I remove the echo. I will try Milens answer. –  Apr 15 '14 at 11:30
  • It's hard to find the bug when not all your code is visible. But simply look for anywhere you output things on the screen... it could be normal HTML that is being processed before your header(); or an echo... – Ronnie Jespersen Apr 15 '14 at 11:32
  • Yeah, this is a nasty problem - even a blank character(space) somewhere in a file might cause this. – Milen Georgiev Apr 15 '14 at 11:35
  • It's there now. It's a bit messy atm. There are some of the stuff in there I no longer use. –  Apr 15 '14 at 11:43
  • Ah, never mind. I have fixet it now. :-) Thanks for leading me in the right direction guys. –  Apr 15 '14 at 11:52
  • 1
    @Adnaves you clearly output a lot of code on the screen before actually getting to the part where your header(); is located. You need to put your if statement at the top of the PHP file before you output anything to the screen. Glad that we could help.. Good luck with the project :) – Ronnie Jespersen Apr 15 '14 at 11:52
1

Do you send(output/echo) anything before header('Location: '.$_SERVER['PHP_SELF']); ?

If that is the case, you will have to turn the object buffer on. Using header, you always have to send the headers first or it will not work.

Milen Georgiev
  • 502
  • 3
  • 13