1

I have a index page which checks to see if a session variable has been set and if so it redirects (using 'header') to the 'customer' page. This works on my local host but does not work when I upload the site to GoDaddy:

<?php
include("Classes.php");
session_start();
include("connector.php");

if ($_SERVER['REQUEST_METHOD'] == "GET"){
    session_unset();
}

    if ($_SERVER['REQUEST_METHOD'] == "POST" && isset($_POST['accname'])){
        $name = $_POST['accname'];
        $password = $_POST['password'];
        $_SESSION['customer'] = new customer($name,$password);
        echo $_SESSION['customer']->custno;
        if (isset($_SESSION['customer']->custno))  {
               header("Refresh:0;customer.php");
        }
    }  
John
  • 45
  • 4
  • Can you explain what does not work and what happens instead? – Guido Leenders Jan 15 '17 at 08:48
  • Possible duplicate of [How to fix "Headers already sent" error in PHP](http://stackoverflow.com/questions/8028957/how-to-fix-headers-already-sent-error-in-php) – M. Eriksson Jan 15 '17 at 08:48
  • 1
    `if isset($_SESSION['customer']->custno))` - That's not valid PHP syntax. You're missing the initial `(`. You should check your error log. – M. Eriksson Jan 15 '17 at 08:50
  • `&& sset($_POST['accname'])` - spelling error on `isset()`. Is this a bad rewrite of your code just for SO or is it an actual copy/paste of the real code? You _really really_ need to check your error log, and you should _really_ turn on `display_errors` in your local environment while developing. – M. Eriksson Jan 15 '17 at 08:57
  • Ok the two code errors were due to me typing in stackoverflow, they dont occur in the code. So the line should and does read: if (isset($_SESSION['customer']->custno)) and this bit does read: && isset($_POST['accname']) So when I run this code on my localhost, it detects that SESSION['customer'] is set, and redirects to the customer.php page. But when I upload it to GoDaddy, it doesn't create an error, it just never leaves the index.php page. – John Jan 15 '17 at 09:09
  • Always copy/paste the actual code instead of retyping it here on SO. It's impossible for us to find potential errors/problems with the code if we don't get to see the real thing. The redirect shouldn't work, since you're outputting to the screen before you're adding a header (all headers must be sent _before_ you output data to the screen). – M. Eriksson Jan 15 '17 at 09:26
  • Well I did copy and paste to SO, but something went wrong with the formatting. Really the problem is very simple. This line: if (isset($_SESSION['customer']->custno)) { header("Refresh:0;customer.php"); } works and redirects me to the customer page when I run the code on localhost, but does not work when I run it on GoDaddy. Anyone? – John Jan 15 '17 at 09:29

2 Answers2

0

You have the header wrong but it also may complain about the header after content.

 header("refresh:5;url=yourpage");
CodingInTheUK
  • 930
  • 7
  • 16
  • Why not: `header('Location: url')` if you're just gonna redirect the user? And there are more issues with the code. – M. Eriksson Jan 15 '17 at 08:53
  • It's not possible to guess the reason that refresh was used instead so i have not changed it. The other issues appear to be getting pointed out already in the comments of the question. – CodingInTheUK Jan 15 '17 at 08:58
  • Ok so I've corrected the slight errors in the code (these were errors typing it into stackoverflow, not in the original code). The bottom line is when I run this code on my localhost and it comes to the line where it says: if (isset($_SESSION['customer']->custno)) { header("Refresh:0;customer.php"); } it then redirects to the customer.php page. But it does not redirect when I upload this code to a live server on Godaddy. – John Jan 15 '17 at 09:16
  • Ok I give up. Its simply that header("Refresh:0;customer.php"); works on my localhost (redirects you to customer page) but does not work when I upload to GoDaddy. I'll put in the too hard basket. – John Jan 15 '17 at 09:44
0

The answer is: I give up. Seems that code that works on localhost just doesn't work when you put it up on the web. PS there was nothing wrong with the code: minor errors when pasteing into SO which were corrected.

John
  • 45
  • 4