0

I am quite new to php and I tried to find an answer for this but couldn't, if there is an answer already then I apologize.

I am building a simple website, just a few pages (login, register, home).

On the register form, once the submit button is pressed a PHP script checks if the email used for registration is already in the database, if it is I need to display some feedback to the user.

I have two files, a.php which is really just an html file for the page and b.php which is my script.

I am using session_start() in both of the files and I am displaying a $_SESSION variable in the html page which contains the feedback (i.e "Email already used").

However I keep getting an uninitialized variable warning since when the html page is first opened the $_SESSION variable does not exist yet.

How do I fix this? I do not want to use JS or AJAX at this stage, and would like to keep the files separate. I am aware of what the error means.

This is the code in a.php (the html page)

<p><?php session_start(); echo $_SESSION["message"];?></p>

This is the code in b.php (the script)

if($row){
    $_SESSION["message"] = "Email already used";
    header( 'Location: register.php' );
    exit();
}

Thank you.

EDIT - SOLUTION: Adding

if(isset($_SESSION["message"])){echo $_SESSION["message"];}

fixes the issue.

valegians
  • 850
  • 1
  • 7
  • 17
  • You want us to tell you what's wrong with your code without showing us the code? – Matt Mar 28 '17 at 15:16
  • @Goofynose You should post your codes as well so that we can reproduce it and fix the problem... – Ratul Doley Mar 28 '17 at 15:23
  • I added the code. Please don't mark as duplicate, I know what the error is and how to remove it -> initialize the variable in the html file, but doing so just resets it and prevents me from using the value passed by the script. – valegians Mar 28 '17 at 15:24
  • 1
    Hey goofynose, before you echo `$_SESSION["message"]` check if the session variable is set as `if(isset($_SESSION["message"])) echo $_SESSION["message"];`. – Ratul Doley Mar 28 '17 at 15:32
  • @RatulDoley Thank you very much that fixes it! :) – valegians Mar 28 '17 at 15:35
  • Then mark the comment as useful... – Ratul Doley Mar 28 '17 at 15:37

0 Answers0