Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

PHP Vlucas doesn't work.

Laleesh

Coder
I'm using Vlucas library to set up ENVs and I followed the guide from a readme file but don't understand why it's not working.
I don't get a php error log, just get redirected to a 505 page. Before using Vlucas, I recieved PHP error log and it's echo message.
Script is working fine with hard code.
.env file is placed inside the root directory.

PHP:
use Dotenv\Dotenv;
$dotenv = Dotenv\Dotenv::createImmutable("../");
$dotenv->load();

$mail->Username = $_ENV["email"];
$mail->Password = $_ENV["password"];


Full script:

<?php
if (isset ($_SERVER ["HTTPS"]) && $_SERVER ["HTTPS"] !== "off") {
    header("Strict-Transport-Security: max-age=31536000; includeSubDomains; preload");

    header("Content-Security-Policy: default-src 'self';
    script-src 'self' https://www.google-analytics.com https://ssl.google-analytics.com https://www.googletagmanager.com;
img-src 'self' https://www.google-analytics.com;
connect-src 'self' https://www.google-analytics.com;");

}

if ($_SERVER["REQUEST_METHOD"] == "POST") {
    $industry = filter_input(INPUT_POST, "industry", FILTER_SANITIZE_SPECIAL_CHARS);
    $purpose = filter_input(INPUT_POST, "purpose", FILTER_SANITIZE_SPECIAL_CHARS);
    $info = filter_input(INPUT_POST, "info", FILTER_SANITIZE_SPECIAL_CHARS);
    $select = filter_input(INPUT_POST, "webType", FILTER_SANITIZE_SPECIAL_CHARS);
    $webPurpose = filter_input(INPUT_POST, "webPurpose", FILTER_SANITIZE_SPECIAL_CHARS);
    $name = filter_input(INPUT_POST, "name", FILTER_SANITIZE_SPECIAL_CHARS);
    $clientEmail = filter_input(INPUT_POST, "email", FILTER_SANITIZE_EMAIL);
}

if (!filter_var($clientEmail, FILTER_VALIDATE_EMAIL)) {
    die("Invalid email.");
}

require "../vendor/autoload.php";
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use Dotenv\Dotenv;

$dotenv = Dotenv\Dotenv::createImmutable("../");
$dotenv->load();
$mail = new PHPMailer();

$mail->isSMTP();
$mail->isHTML(true);
$mail->SMTPDebug = SMTP::DEBUG_SERVER;
$mail->Host = 'smtp.gmail.com';
$mail->Port = 465;
$mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS;
$mail->SMTPAuth = true;
$mail->Username = $_ENV["email"];
$mail->Password = $_ENV["password"];

$mail->setFrom("[email protected]");
$mail->addAddress("[email protected]");
$mail->Subject = "New Submission!";
$mail->Body = "Industry - " . $industry . "<br>" . "Purpose - " . $purpose . "<br>" . "Additional information - " . $info . "<br>" . "Web type - " . $webPurpose . "<br>" . "Management - " . $managment . "<br>" . "Name - " . $name . "<br>" . "Email - " . $clientEmail;

if ($mail->send()) {
    header("location: ../mail_submitted.html");
    exit();
} else {
    echo "Sorry, something went wrong. You can try submitting again, or contact me directly at [email protected]";
};
 
Solution
I don't know anything about Vlucas Dotenv, but what do environment variables have to do with a server status 505 ?
I would look in the server logs for more information about the 505 error.
That is about all I can say. Are you on an up to date version and is your gmail configured to be used for email like that? X E.
I updated my server and I still get the error.
My Gmail does support this kind of transfer because I can do it with hard-coded ENVs.
 
I don't know anything about Vlucas Dotenv, but what do environment variables have to do with a server status 505 ?
I would look in the server logs for more information about the 505 error.
 
Solution
I don't know anything about Vlucas Dotenv, but what do environment variables have to do with a server status 505 ?
I would look in the server logs for more information about the 505 error.
It gave me this log that is relevant:
Code:
[Thu May 23 12:53:40.426263 2024] [php:error] [pid 252343] [client 37.203.111.232:50392] PHP Fatal error:  Uncaught Error: Class "Dotenv\\Dotenv\\Dotenv" not found in /var/www/laleesh/web_design/web_design_form.php:31\nStack trace:\n#0 {main}\n  thrown in /var/www/laleesh/web_design/web_design_form.php on line 31, referer: http://laleesh.com/web_design/web_design.html

I don't know why it's searching for Dotenv\\Dotenv\\Dotenv when guides show to
Code:
use Dotenv\Dotenv
Do I put that inside a use function?
 
It gave me this log that is relevant:
Code:
[Thu May 23 12:53:40.426263 2024] [php:error] [pid 252343] [client 37.203.111.232:50392] PHP Fatal error:  Uncaught Error: Class "Dotenv\\Dotenv\\Dotenv" not found in /var/www/laleesh/web_design/web_design_form.php:31\nStack trace:\n#0 {main}\n  thrown in /var/www/laleesh/web_design/web_design_form.php on line 31, referer: http://laleesh.com/web_design/web_design.html

I don't know why it's searching for Dotenv\\Dotenv\\Dotenv when guides show to
Code:
use Dotenv\Dotenv
Do I put that inside a use function?
It says the error is on line 31, which is

$dotenv = Dotenv\Dotenv::createImmutable("../");

So I guess this may need to be

$dotenv = Dotenv::createImmutable("../");

I find it strange that you should get a 505 error page. 500 - HTTP Version Not Supported. Are you sure it's not 500 – Internal Server Error ?
 
It says the error is on line 31, which is

$dotenv = Dotenv\Dotenv::createImmutable("../");

So I guess this may need to be

$dotenv = Dotenv::createImmutable("../");

I find it strange that you should get a 505 error page. 500 - HTTP Version Not Supported. Are you sure it's not 500 – Internal Server Error ?
Oh, I'm sorry, it is 500...
I tried using
Code:
$dotenv = Dotenv::createImmutable("../");
, but still doesn't work.
What I find strange is the error it's giving me inside the log. I literally followed instructions for this thing.
I might try to use Linux ENV instead of a .env file tomorrow...
 
Oh, I'm sorry, it is 500...
Oh ok. Please be more careful when describing your issue. Giving wrong information is a waste of time.

I might try to use Linux ENV instead of a .env file tomorrow...
I'd try and solve the problem, rather than walk away from it. You wrote "Vlucas does not work" but you don't know that (wrong info again). The problem is that your PHP cannot find it. My guess is you put the files in a wrong path. Run strace on your webserver process and see what it is trying to open and what the result was.
 
Oh ok. Please be more careful when describing your issue. Giving wrong information is a waste of time.


I'd try and solve the problem, rather than walk away from it. You wrote "Vlucas does not work" but you don't know that (wrong info again). The problem is that your PHP cannot find it. My guess is you put the files in a wrong path. Run strace on your webserver process and see what it is trying to open and what the result was.
I like that spirit and sorry again.

I used the strace command on the root PID and all I get is
Code:
wait4(-1, 0x7ffd3f21a344, WNOHANG|WSTOPPED, NULL) = 0
times({tms_utime=143 /* 1.43 s */, tms_stime=358 /* 3.58 s */, tms_cutime=18 /* 0.18 s */, tms_cstime=20 /* 0.20 s */}) = 1820403175
pselect6(0, NULL, NULL, NULL, {tv_sec=1, tv_nsec=0}, NULL^Cstrace: Process 25929
repated, no change regardless of what I do.

I'm a beginer, this is way above my pay grade...
 
I used the strace command on the root PID and all I get is
You should be specifying the PID of the webserver process, not the root PID. Enter this command

strace -f -p [pid-of-webserver] -o /tmp/strace.log

(probably best done as root).
Do your test that produces the error.
Terminate strace with CTRL-C
Then do
grep Dotenv /tmp/strace.log
and let's see what we get.

I'm a beginer, this is way above my pay grade...
Yeah, strace is a little bewildering if you're a beginner. But it's the best tool you could wish for.
 
You should be specifying the PID of the webserver process, not the root PID. Enter this command

strace -f -p [pid-of-webserver] -o /tmp/strace.log

(probably best done as root).
Do your test that produces the error.
Terminate strace with CTRL-C
Then do
grep Dotenv /tmp/strace.log
and let's see what we get.


Yeah, strace is a little bewildering if you're a beginner. But it's the best tool you could wish for.
There's quite a few pids, I don't know which one i need.
One of them is root, other ate che2.
 
Because you have set use Dotenv\Dotenv; in the code, you then only reference that class as purely Dotenv. You then need to update this code to look like this...

Code:
$dotenv = Dotenv::createImmutable("../");
 
Because you have set use Dotenv\Dotenv; in the code, you then only reference that class as purely Dotenv. You then need to update this code to look like this...

Code:
$dotenv = Dotenv::createImmutable("../");
I had already suggested the exact same thing, if you read the thread above. Apparently it did not help.
Thinking about that, it could be that either the browser or the webserver is caching the PHP file so a change is not seen. Worth trying to clear temporary files and/or restart the web server.
 
Back
Top Bottom