I just transfer my PHP app, I used to have my app on example.com/app but now I transferred the app to its own domain, othersite.com. My principal site and PHP site are on the same server with apache virtual hosts, it runs on ubuntu 20.xx. The app works fine but I had problems with PHP sessions, when I checked my apache error.log found this
PHP Notice: Undefined index: user_type in /var/www/othersite.com/public_html/index.php on line 66, referer: https://example.com/
and this is what I have on that line,
if( $_SESSION['user_type'] == 'admin' || $_SESSION['user_type'] == 'doctor' || $_SESSION['user_type'] == 'nurse' ) { drawDashboardChecks();}
What do you think could be the problem here? I didn't build the app but I guess the code is fine because it worked on the previous site and for some reason, the error log shows me a reference to my principal site. NOTE: I translated the values on the PHP line. Thank you.
CodePudding user response:
Well the notice tells - you don't have that user_type
key in your array. If its fine, change the check in the way like you don't actually expect user_type
to always be there:
if(
!empty($_SESSION['user_type'])
and in_array($_SESSION['user_type'], ['admin','doctor','nurse']
){
....
CodePudding user response:
First there should be checked isset condition..
if(isset($_SESSION['user_type'])){
//code
}
CodePudding user response:
Check where the Requests are now have been sending, this might be caused because of the URLS that is written in the Forms. Please look at this example:
<form action="http://example.com/app/login.php" method="POST">
<input type="email" placeholder="email">
<input type="password" placeholder="password">
<form>
And in your login.php page you had a code to handle the request. If the $_POST['email']
is received, then this simply means that it can be handled, if it's not received it's simply undefined index, and you see the error that you are now receiving.
Look at the url bellow.
<form action="http://otherwebsite.com/app/login.php" method="POST">
<input type="email" placeholder="email">
<input type="password" placeholder="password">
<form>
It should be something like this instead.
<form action="http://otherwebsite.com/login.php" method="POST">
<input type="email" placeholder="email">
<input type="password" placeholder="password">
<form>
So please check where are you sending your request before you try to handle them, and the best practice is to use isset()
php build in function before dealing with indexs.
if(isset($_POST['email'])) {
$email = trim($_POST['email']);
} else {
$email_err = "Email is required";
}
Hope you find this helpful.
Happy Coding!