0

im trying to deal with error catches and sending the user to a error page upon any errors , im also trying to make sure that the session is started if it is the first and only apperance at the page the user starts at.

from this code, how could i remove the need to try catch every part of the page. i was thinking along the lines of maybe a universal error catch that sends user to an error page upon any error, along with the error data.

So far iv placed every part of the page in try catch , but it seems unecessery. am i right in thinking this, or is alot more secure to check every part of the page this way.

Also the Session_start(); is confusing me, Does it only need to be called once, or should it be called whenever it is not set. if thats the case how do i ensure no matter what page the user begins on , the session will be started. when the session is allready started i cannot recall it on everypage. so how do i check if the session has started or not... Currently i am looking for $_SESSION to have been filled, but that may not be the case on all occassions of every page. im just wondering is there another way to check if Session_Started();

is session started set in the users cache??? so if the user came back to the website and bypassed index.php then session would not need to be started. or should i be checking on evvery page for session to be started?

Any help is greatly appreciated, im new and learning as much as i can but often i come across these instances where only human interaction can advise.

<div class='MainContainerDiv'> 
        
                    <?php 
                    if (!isset($_SESSION)){session_start();}//session_start should only be called once

                        try{
                            $DBConnect='DBmanagment/DBConnect.php';
                            include $DBConnect;
                            }
                            catch(exepetion $e){ 
                                $_SESSION['ConnectionError'] = $e->getMessage();

                                header("location: pages/ERRORS/ERRORPAGE1.php");
                                exit();die();
                            };


                                try{
                                    $selectedpage="pages/landingpage/Landingpage.php";
                                    if (!isset($selectedpage)) {$selectedpage='pages/landingpage/Landingpage.php';} else {$selectedpage=$selectedpage;}

                                    include $selectedpage;
                                    }
                                    catch(exepetion $e){ 
                                        $_SESSION['ConnectionError'] = $e->getMessage();

                                        header("location: pages/ERRORS/ERRORPAGE1.php");
                                        exit(); die();

                                        };
                    


                    ?>
                    
            </div>
Shane Dee
  • 1
  • 4

1 Answers1

0

The sesion_start() can only be called once per execution. If you call it more than once, it will show some error message. You can check if the session has been initiated, but the "right" thing to do is to have a clean structure where the database connections, session_start() are handled only by one file that is included in all the other pages.

If you don't care about that, you can check the session with:

<?php
if(session_status() == PHP_SESSION_NONE){
    //session has not started
    session_start();
}

About the try/catch thing, in PHP if you find yourself making a lot of try/catch, probably you are doing something wrong. I know that in other languages (like Python) is normal to try to include a file and handle the error, but in PHP the common practice is to check if the file exits with an if statement and thefile_exists built-in function. If the file exists, you can include it and if not, you handle the error in the else part of your code.

In python the philosophy is: It is better to ask for forgiveness than to ask for permission

While in PHP is: Better safe than sorry.

Just a nice guy
  • 549
  • 3
  • 19
  • thank you, il look into it and ammend my code. i lack the php function knowledge, so im learning as i go along unfortunatly, and im presented with a new set of problems everyday. – Shane Dee Mar 29 '21 at 03:15
  • This is a normal part of the process. Just keep working, if you don't find any problem while learning I would say that you are not really trying. Also, if my answer helped you remember to check it as the solution to your question. – Just a nice guy Mar 29 '21 at 03:22
  • Also exit() die() is redundant – Julien B. Mar 29 '21 at 03:22
  • Much appreciated. – Shane Dee Mar 29 '21 at 03:34
  • What must i use in replace of Die() exit() - i checked here, but its a little inconclusive for me. https://rules.sonarsource.com/php/RSPEC-1799 – Shane Dee Mar 29 '21 at 03:34
  • Its pretty much the same. More details here: https://stackoverflow.com/questions/1795025/what-are-the-differences-in-die-and-exit-in-php – Just a nice guy Mar 29 '21 at 03:38