Jump to content

[SOLVED] Setting up session variable and page redirection


cliftonbazaar

Recommended Posts

Brand new to PHP (just switched from ASP) so please be patient as I convert all my pages  ;D

 

I have 2 pages - register.php and adduser.php

On the first page the user is to fill out all their information (name and email mostly), the second page checks the information to make sure it is 'valid' before it adds the information to the database, the first check is to make sure they have entered and email address (my access database was full of blank entries  :-\ ).

 

The code I tried for register.php is

<html>
<?PHP
  Session_start();
  IF (!empty($_Session["error"]))  echo $_Session["error"];
?>
<head>
//Rest of the code

 

Code for adduser.php -

<?PHP
  IF ($_POST["email"] == "") {
    header('Location:http://www.cliftonbazaar.com/register.php');
    $_Session['error'] = "You have not stipulated an email address.";
    die();
  }
?>
//THERE IS NO OTHER CODE FOR THIS PAGE AT THIS TIME

 

This seems to redirect the user succesfully back to the register page if there is no email addy but the error message is not displayed.

You should be getting header warnings/errors.

 

In register.php, you have output before session_start() in the form of "<html>" which is not allowed.

 

register.php

<?php
ini_set('display_errors', 1);//use these while debugging
error_reporting(E_ALL);//use these while debugging

session_start();
if(!empty($_SESSION['error'])){
  echo $_SESSION['error'];
}
?>

<html>
<head>
...

 

@mrMarcus - it only has to be present before any SESSION variable are accessed, and before anything is output to the page (when the server finishes processing and begins to send it back to the browser).

Thank you both for your replies, I'm currently learning by reading a book so I have a long way to go :)

 

ini_set('display_errors', 1);//use these while debugging
error_reporting(E_ALL);//use these while debugging

Didn't read anything about these lines but now I'll be using them full time, makes life a 100 times easier  :D

 

Also learnt the hard way that SESSION needs to be all upper caps  :-[

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.