r/PHPhelp • u/Zealousideal-Bath-37 • Apr 14 '25
My code displays content only on VCCode console but not on Chrome Browser
This code https://paste.mod.gg/wdkejsugoxan/0 seems to be running on VCCode console but definitely not on Chrome Browser. As it runs (with a bunch of warnings) on VCCode, I assume that the code was able to connect to my database. However it points to 500 internal server error if I open this code on Chrome Browser (http://localhost:84/DB_v1.php -- this is the url I used). I am on XAMPP/VSCode combo and am very new to this. I don't think localhost:84 could be wrong as the other php code have been successfully loaded and has displayed html content. I specifically set it to point to port 84 instead of standard localhost.
Could anyone tell me what could have gone wrong in my specific set up?
Below is my output on VCCode Console if this helps
[Running] php "/Applications/XAMPP/xamppfiles/htdocs/fileUpload_Vorlesung_Ver1/DB_v1.php"
<tr><th>alles ausgeben</th></tr><tr><td>Wert für A1</td><td>Wert für B1</td><td>Wert für C1</td><td>Wert für D1</td></tr></table>
Warning: session_start(): Session cannot be started after headers have already been sent in /Applications/XAMPP/xamppfiles/htdocs/fileUpload_Vorlesung_Ver1/DB_v1.php on line 29
Warning: Undefined global variable $_SESSION in /Applications/XAMPP/xamppfiles/htdocs/fileUpload_Vorlesung_Ver1/DB_v1.php on line 30
Warning: Trying to access array offset on null in /Applications/XAMPP/xamppfiles/htdocs/fileUpload_Vorlesung_Ver1/DB_v1.php on line 30
Warning: Undefined array key "paymentmethods" in /Applications/XAMPP/xamppfiles/htdocs/fileUpload_Vorlesung_Ver1/DB_v1.php on line 31
Warning: Cannot modify header information - headers already sent by (output started at /Applications/XAMPP/xamppfiles/htdocs/fileUpload_Vorlesung_Ver1/DB_v1.php:20) in /Applications/XAMPP/xamppfiles/htdocs/fileUpload_Vorlesung_Ver1/DB_v1.php on line 35
Warning: Undefined array key "lastvisit" in /Applications/XAMPP/xamppfiles/htdocs/fileUpload_Vorlesung_Ver1/DB_v1.php on line 37
TestPHP Warning: session_start(): Session cannot be started after headers have already been sent in /Applications/XAMPP/xamppfiles/htdocs/fileUpload_Vorlesung_Ver1/DB_v1.php on line 29
PHP Warning: Undefined global variable $_SESSION in /Applications/XAMPP/xamppfiles/htdocs/fileUpload_Vorlesung_Ver1/DB_v1.php on line 30
PHP Warning: Trying to access array offset on null in /Applications/XAMPP/xamppfiles/htdocs/fileUpload_Vorlesung_Ver1/DB_v1.php on line 30
PHP Warning: Undefined array key "paymentmethods" in /Applications/XAMPP/xamppfiles/htdocs/fileUpload_Vorlesung_Ver1/DB_v1.php on line 31
PHP Warning: Cannot modify header information - headers already sent by (output started at /Applications/XAMPP/xamppfiles/htdocs/fileUpload_Vorlesung_Ver1/DB_v1.php:20) in /Applications/XAMPP/xamppfiles/htdocs/fileUpload_Vorlesung_Ver1/DB_v1.php on line 35
PHP Warning: Undefined array key "lastvisit" in /Applications/XAMPP/xamppfiles/htdocs/fileUpload_Vorlesung_Ver1/DB_v1.php on line 37
4
u/flyingron Apr 14 '25 edited Apr 14 '25
Move the sesion_start() up before writing any headers or body text. $_SESSION is set by session_start.
The "failed to load" page is a result of the unhandled PHP errors. If you want to see the errors printed, you need to tell them to print either in you php.ini or by calling something like error_reporting(E_ALL) early on.
example,... put the following right after the first <?php in your code:
4
u/Big-Dragonfly-3700 Apr 14 '25
Hare is a list of programming practices that will help with the errors (repeats some information already given) and eliminates a lot of typing in this code.
The code for any page should be laid out in this general order -
As already posted, session_start() needs to go in the initialization section. If you are putting php's error related settings into your code, they should be before all other php code. Note: you cannot set display_startup_errors in your code, because php has already started by that point.
The database specific code, that's querying for and fetching the data from the query goes in the get method business logic section. You should fetch the data into a php variable, to be used later. This separates the database specific code from the presentation code.
The code testing if there is any data and producing the output from that data goes in the html document section.
Some more points -
<table>tag and you should be using css to style the web page (the border attribute is obsolete.)1
u/MateusAzevedo Apr 14 '25
A 500 status code usually means a PHP fatal error. You can look at the PHP logs to see what the error is, or configure php.ini to display errors on screen.
Note: all the messages you got from CLI are warnings, non-fatal errors that don't stop PHP execution and should not cause a 500 response code. This means it's very likely you're getting a different error when calling the file from a web request, hence why you need to check logs or set display errors.