Forum Moderators: coopster

Message Too Old, No Replies

php myslq login issue - used to work

php myslq login issue

         

janprocnj

3:55 pm on Dec 8, 2021 (gmt 0)

5+ Year Member



I had code that worked fine for a year, then something happened. Maybe the hosting company upgraded the PHP version, not sure. I then moved the code to a new hosting company and have what I believe to be the same database that I replicated. If an improper code is entered it does display the error message, but if the correct code is entered, nothing happens. I commented out the redirect because that doesn't seem to work, but it used to work. Is my PHP redirect code deprecated ? Any assistance is appreciated. thanks




<?php
session_start();
include("config.php");


if($_SERVER["REQUEST_METHOD"] == "POST") {
// username and password sent from form


$mypassword = mysqli_real_escape_string($conn,$_POST['password']);
$_SESSION['password'] = $mypassword;



$sql = "SELECT id FROM tblHotelCard2 WHERE PASSCODE = '$mypassword' AND ACTIVATED='1'";
$result = mysqli_query($conn,$sql);
$row = mysqli_fetch_array($result,MYSQLI_ASSOC);

$active = $row['active'];

$count = mysqli_num_rows($result);

// If result matched $myusername and $mypassword, table row must be 1 row

if($count == 1) {
echo "test hello world";
session_register("myusername");
$_SESSION['login_user'] = $myusername;
$_SESSION["hotelcard"] = "YES";
$_SESSION["password"] = $mypassword;
// header("location: hotel-enter.php");



}else {

$error = '<button type="button" class="btn btn-danger">The code you entered is either invalid or was previously used.</button><br><br>';
}
}
?>

robzilla

8:07 pm on Dec 8, 2021 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



You'd probably be notified via an error message or error log entry if any of the functions were deprecated.

What I usually do if I can't figure out where things go wrong is do an echo()/print_r()/var_dump() of variables or a comment in various places throughout the script, so that I can drill down to the source of the issue.

For example, if you put a print_r($_SESSION); after $_SESSION['password'] = $mypassword; and it actually holds the password, you'll know everything's OK up to that point. You move on to the MySQL query, is it returning any data? And so forth.

Assuming the code is working OK, the problem is probably either that $_SERVER["REQUEST_METHOD"] does not equal POST, or more likely that $count does not equal 1. If you think that should give you an error, please note that $error is set but not actually being printed to the page with echo().

janprocnj

8:34 pm on Dec 8, 2021 (gmt 0)

5+ Year Member



Robzilla - Thanks. I will try to debug this further later tonight. I did google this error and some threads said to use this for a redirect. This code does work -->

echo "<script type='text/javascript'> document.location = 'welcome.php'; </script>";

however I am wondering if a browser has javascript disabled for whatever reason, this would not work?

NickMNS

8:50 pm on Dec 8, 2021 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



This might have to do with your leading slash.
echo "<script type='text/javascript'> document.location = 'welcome.php'; </script>";

The code above will lead to different location depending on where the user is.
If the user is at:
https://example.com/interesting-stuff/index.html

then the script will redirect the user to:
https://example.com/interesting-stuff/welcome.php

But if the user is on the home page:
https://example.com/index.html

then the script will redirect the user to:
https://example.com/welcome.php

And I don't think that is what you want. It could be that the same issue is occurring in your PHP. Then when you changed hosts, you changed some portion of your folder structure thus causing the issue.
This should likely be:
header("location: hotel-enter.php"); //wrong
header("location: /hotel-enter.php"); //wrong


I have very little experience with PHP so I'm not sure that this is the case but others here are sure to chime in.

robzilla

9:03 pm on Dec 8, 2021 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



This code does work -->

echo "<script type='text/javascript'> document.location = 'welcome.php'; </script>";

That's instead of the // header("location: hotel-enter.php"); I assume? You're aware that the // in front of the header() function prevents it from running, because it's now a comment?

however I am wondering if a browser has javascript disabled for whatever reason, this would not work?

Correct. And ideally a redirect is initiated server-side (by PHP or the web server), not client-side (in the browser, e.g. with javascript).

In regards to the Location header, an absolute path is most widely supported but a relative one usually works:
Most contemporary clients accept relative URIs as argument to » Location:, but some older clients require an absolute URI including the scheme, hostname and absolute path.

[php.net...]

janprocnj

9:46 pm on Dec 8, 2021 (gmt 0)

5+ Year Member



Rob - yes, I commented it out because it didn't work. Very frustrating... same code on another blank PHP page worked, so it must have to do with the order in which things are happening on the page. For now, the javascript code is working

Nick - folder structure is the same. Nothing will forward... not even if I put a full URL in there to another web site.

robzilla

10:39 pm on Dec 8, 2021 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



That reminds me, if you're setting headers from PHP you can't do so after outputting anything to the page:
Remember that header() must be called before any actual output is sent, either by normal HTML tags, blank lines in a file, or from PHP. It is a very common error to read code with include, or require, functions, or another file access function, and have spaces or empty lines that are output before header() is called. The same problem exists when using a single PHP/HTML file.

[php.net...]

In the above example, you're echoing "test hello world" before calling header(), so that's not going to work.

However, that's not something's that's changed in PHP recently.

robzilla

10:16 am on Dec 9, 2021 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



This may not be presented as an error on the page, depending on how you set error reporting, because it's officially a warning.

If you check your error logs, you may find an entry like this: "Warning: Cannot modify header information - headers already sent"