Page Not passing variables as it did

So I had no issues before the crash the other day. Now when I try to login for some reason my pages are not passing the variables which is a critical part of my site.
Here is my login code which nothing changed other than the page redirection:

<!-- Bootstrap Core CSS file -->
    <link rel="stylesheet" href="assets/css/bootstrap.css">

    <!-- Override CSS file - add your own CSS rules -->
    <link rel="stylesheet" href="assets/css/styles.css">

</head>
<body>

    <!-- Include Nav Bar HTML -->
    <?php
    include 'navBarBeforeLogin.html';
    ?>

    <!-- Page Content -->
    <div class="container-fluid">
        <div class="row">
            <div class="col-sm-12">

                <!-- Page breadcrumb -->
                <ol class="breadcrumb">
                    <li><a href="index.php">Home</a></li>
                </ol>

                <!-- Page Heading -->
                <h1>CS & IT Department Inventory - Login</h1>
                <hr>

                <!-- Create Account Form -->
                <h3>Log Into Inventory</h3>
                <div class="well">
                    <form name="login" id="login" method="POST" action="<?php $_SERVER['PHP_SELF'] ?>">
                        <div class="form-group">
                            <label for="userName">User Name</label>
                            <input name="userName" type="text" class="form-control" id="userName" placeholder="Enter your user name" required="" maxlength="100">
                        </div>
                        <div class="form-group">
                            <label for="password">Password</label>
                            <input name="password" type="password" class="form-control" id="password" placeholder="Enter a password" required="" maxlength="100">
                        </div>
                        <button name="submit" type="submit" class="btn btn-primary">Submit</button>
                    </form>
                    <?php
                    //check to see if 
                    if ((isset($_POST['submit']))){
                        
                        
                        //include database handler file so that we can
                        //call it's functions
                        include 'dbHandler.php';
                    
                        //getting data input into the form and assigning
                        // it to variables
                        $userName = $_POST['userName'];
                        $password = $_POST['password'];
                        
                        //connecting to MySQL and selecting shopper database
                        $link = connect();
                        
                        $status = select_user($link, $userName, $password);
                        
                        if ($status == 0) {
                            echo '<p class="help-block">You\"re not a valid user of Inventory!</p>';
                        } else{
                            $_SESSION['userid'] = $status;
                            close($link);
                            ?>


                            <script type="text/javascript">
                            window.location.href = 'http://localhost/Inventory/HomePage.php';
                            </script>


                            <?php
                            exit();
                            }
                            //close connection to MySQL
                        close($link);
                        } 
                        ?>
                </div>
                <hr>

            </div>
        </div>
        <!-- /.row -->
    </div>

Try adding some }else{ statements to know what happens.

Iā€™m not quite sure I understand what you mean because each one of my pages is in an if else. If the userid is set and isnt 0 then it continues. Unless maybe for some reason my database was changed in the update

can you try this in the js replace
window.location.href = 'path-of -the-file';
instead of using URL like this
window.location.href = 'Inventory/HomePage.php';
this should work fine cuz right now when you submit your form and the browser runs to the js it sees it as a different website or something like that kind of odd right lol. and make sure the path is right

So rather than path of file if I want it to go to my index page what would i put. Iā€™m sorry Iā€™ve only ever used URLs

just remove the http://localhost and leave the rest i think that should be the path

itā€™s not a good idea to work with urls cuz sometimes crawls will think that you pointing to external link. by using file path to link to your file crawls will know that you are linking to another page on your website.

Thank you. So I fixed that but now its saying my $_SESSION is an undefined variable.

1 Like

did you do session_start();
on the top of your document and also can you cope and paste here the error you getting and thanks

I had that commented out because it wasnt getting to my page as I have it check for userid first. I fixed that now I get this Notice: Undefined index: userid in /storage/ssd5/170/5390170/public_html/index.php on line 40

Like I said everything worked fine before the issue the other day. I didnā€™t touch the code then these issues today

try to put the userid in your select_user function
$status = select_user(user-id,$link, $userName, $password);
cuz maybe the error because you are setting the session userid to a var but you didnā€™t selectect the value of the userid from the database table

I select the userid based on the name and password and set it to status. Then I do $_Session[ā€˜useridā€™] = $status

Iā€™m extremely confused because it worked just fine 3 days ago no issues at all then I tried logging in to test some functionality and I had this issue. I didnā€™t touch the login code at all and i didnā€™t touch the variables code

Hey @Pittsie84 i think you should change this url to your current website url

I have done that thank you. The redirection is working fine but now the new page is not getting the session variable I created

set
session_start();
at the top of your new page

Here is my index page code which is the place where the login will go to:

<?php
session_start();

// if (!isset($_SESSION['userid']) || ($_SESSION['userid'] == 0)) {
//     //redirect the user to the login page
//     header('Location: login.php');
// }
?>
<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8">
        <title>CS & IT Department Inventory</title>

        <!-- Bootstrap Core CSS file -->
        <link rel="stylesheet" href="assets/css/bootstrap.css">

        <!-- Override CSS file - add your own CSS rules -->
        <link rel="stylesheet" href="assets/css/styles.css">

    </head>
    <body>

        <!-- Include Nav Bar HTML -->
        <?php
        include 'navBarAfterLogin.html';
        ?>

        <!-- Page Content -->
        <div class="container-fluid">
            <div class="row">
                <div class="col-sm-12">
                    
                    <!-- Page Heading -->
                    <h1>CS & IT Department Inventory</h1>
                    <hr>
                    <h2>Click on a link below to Either search the database
                    or view edit add and delete within the database</h2>
                    <?php
                    if ($_SESSION['userid'] != 0) {
                    echo'<h3>
                        <li><a href="LoggedIn.php">Edit</a></li>
                        <li><a href="HomeB.php">Search</a></li>
                    </h3>';
                    }
                    ?>

                </div>
            </div>
            <!-- /.row -->
        </div>
        <!-- /.container-fluid -->

        <!-- JQuery scripts -->
        <script src="assets/js/jquery-1.11.2.min.js"></script>

        <!-- Bootstrap Core scripts -->
        <script src="assets/js/bootstrap.min.js"></script>
    </body>
</html>

Now I commented out the first part so I can actually get to the page and find an error which is the error I previously posted

I would recommend empty instead of !isset

Okay, but that didnā€™t fix my issue of not being able to log in and pass variables. I didnā€™t touch it. It worked the day everything was fixed. Then the day after my code is all of a sudden broken. It also works locally just fine. This makes no sense to me