Jump to content

Please help I am new to php and I'm not getting any errors in console - What am I doing wrong?


Recommended Posts

<!DOCTYPE html>

<?php 

// Check if the user is already logged in, if yes then redirect him/her to dashboard page


if(isset($_SESSION["loggedin"]) && $_SESSION["loggedin"] === true){
          print('<div class="page-header">');
          print('<h1>Hi, <b>');
      echo htmlspecialchars($_SESSION["username"]);
      print('</b> Welcome to our site.</h1></div>');

  exit;
    
}

// Define variables and initialize with empty values


$username = $password = $confirm_password = "";
$username_err = $password_err = $confirm_password_err = "";

?>


<html lang="en">
<head>
    <title>Opus Reports Login</title>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.css">
        <style type="text/css">
        body{ font: 12px verdana; text-align: center; }
        </style>
<!--===============================================================================================-->    
    <link rel="icon" type="image/png" href="images/icons/favicon.ico"/>
<!--===============================================================================================-->
    <link rel="stylesheet" type="text/css" href="vendor/bootstrap/css/bootstrap.min.css">
<!--===============================================================================================-->
    <link rel="stylesheet" type="text/css" href="fonts/font-awesome-4.7.0/css/font-awesome.min.css">
<!--===============================================================================================-->
    <link rel="stylesheet" type="text/css" href="fonts/Linearicons-Free-v1.0.0/icon-font.min.css">
<!--===============================================================================================-->
    <link rel="stylesheet" type="text/css" href="vendor/animate/animate.css">
<!--===============================================================================================-->    
    <link rel="stylesheet" type="text/css" href="vendor/css-hamburgers/hamburgers.min.css">
<!--===============================================================================================-->
    <link rel="stylesheet" type="text/css" href="vendor/animsition/css/animsition.min.css">
<!--===============================================================================================-->
    <link rel="stylesheet" type="text/css" href="vendor/select2/select2.min.css">
<!--===============================================================================================-->    
    <link rel="stylesheet" type="text/css" href="vendor/daterangepicker/daterangepicker.css">
<!--===============================================================================================-->
    <link rel="stylesheet" type="text/css" href="css/util.css">
    <link rel="stylesheet" type="text/css" href="css/main.css">
<!--===============================================================================================-->
</head>

<?Php 
     $mycolour = '';
      if (!empty( $_COOKIE['favcolour'])) {
         $mycolour = $_COOKIE['favcolour'];
     } 
     
?>

<body style="background-color: <?Php echo $mycolour;?>;">
      
    <div class="limiter">
        <div class="container-login100">
            <div class="wrap-login100">
                <form class="login100-form validate-form">
                    <span class="login100-form-title p-b-43" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" method="post">
                        Login to Reports 
                    </span>
                    
                
                    <div class="wrap-input100 validate-input" data-validate = "A Current Valid UserID is required for access:e.g.  1234">
                        <input class="input100" type="text" name="userid" id="userid" value="Admin" autocomplete="on">
                        <span class="focus-input100"></span>
                        <span class="label-input100">UserId</span>
                    </div>
                    
                    
                    <div class="wrap-input100 validate-input" data-validate="A Password is required">
                        <input class="input100" type="password" name="password" id="password" maxlength="8" autocomplete="on">
                        <span class="focus-input100"></span>
                        <span class="label-input100">Password</span>
                    </div>

                    <div class="flex-sb-m w-full p-t-3 p-b-32">
                        <div class="contact100-form-checkbox">
                            <input class="input-checkbox100" id="ckb1" type="checkbox" name="remember-me">
                            <label class="label-checkbox100" for="ckb1">
                                Remember me
                            </label>
                        </div>

                        <div>
                            <a href="#" class="txt1">
                                Forgot Password?
                            </a>
                        </div>
                    </div>
            

                    <div class="container-login100-form-btn">
                        <button type="submit" class="login100-form-btn">
                            Login
                        </button>
                    </div>
                    <div class="text-center p-t-46 p-b-20">
                        <span class="txt2">
                            or sign up using
                        </span>
                    </div>

                    <div class="login100-form-social flex-c-m">
                        <a href="#" class="login100-form-social-item flex-c-m bg1 m-r-5">
                            <i class="fa fa-facebook-f" aria-hidden="true"></i>
                        </a>

                        <a href="#" class="login100-form-social-item flex-c-m bg2 m-r-5">
                            <i class="fa fa-twitter" aria-hidden="true"></i>
                        </a>
                    </div>
                </form>

                <div class="login100-more" style="background-image: url('images/bg-01.png');">
                </div>
            </div>
        </div>
    </div>
<?Php 
    $dsn = 'odbc:MY_DSN';
        $user = '8888';
        $pass = '****';
        

//$pdo = new PDO($dsn, $user, $pass);
 //sasql_connect("uid=dba;pwd=sql"); 
try {
   $pdo = new PDO($dsn, $user, $pass);
   $pdo_status = "Sybase Connected Okay" . "\n";
   //echo $pdo_status;
} catch(PDOException $e) {
    error_log($e->getMessage());
    echo 'Connection failed! No connection no queries!: ' . $e->getMessage();
}

/* Enable error reporting for all types except for deprecated and strict. */
error_reporting(E_ALL);
    
    
//if($_SERVER["REQUEST_METHOD"] == "POST"){
   
    // Check if username is empty
    
    if(empty(trim($_POST["userid"]))){
        $username_err = "Please enter a correct userid.";
    } else{
        $username = trim($_POST["userid"]);
    
    }
    
    // Check if password is empty
    
    if(empty(trim($_POST["password"]))){
        $password_err = "Please enter your password.";
    } else{
        $password = trim($_POST["password"]);
    }
    /*  print('<span class="help-block">');
     echo $password_err; 
     print('</span>'); */
    // Validate credentials
    if(empty($username_err) && empty($password_err)){   // No Errors do select 
        // Prepare a select statement
    Console.log($username);
    print_r ($username);
        $sql = 'Select userid,username,userpassword from users where userid = :username';    
        if($stmt = $pdo->prepare($sql)){
            // Bind variables to the prepared statement as parameters
      
            //$stmt->bindParam('s', $param_username, PDO::PARAM_STR);   
            // Set parameters
            $param_username = trim($_POST["userid"]);
            
            // Attempt to execute the prepared statement
        //$stmt->execute(['transactionNo' => $number, 'tillid' => $till]);
            if($stmt->execute(['userid' => $username])){
                // Check if username exists, if yes then verify password
        //echo ($userid); 
        $result=$stmt->fetch(pdo::FETCH_ASSOC);
        If(!$result){
            echo "failed Login";
        }
                if($stmt-> rowCount() == 1){ 
            if($row = $stmt->fetch()){
                        $id = $row["userid"];
                        $username = $row["username"];
                        $hashed_password = $row["password"];
                        if(password_verify($password, $hashed_password)){
                            // Password is correct, so start a new session
                            session_start();
                            
                            // Store data in session variables
                            $_SESSION["loggedin"] = true;
                            $_SESSION["id"] = $id;
                            $_SESSION["username"] = $username;                            
                            $_SESSION['loggedin_time'] = time();  
                            // Redirect user to welcome page perhaps
                            header('location: dashboard.php');
                        } else{
                            // Display an error message if password is not valid
                            $password_err = "The password you entered was not valid.";
                        }
                    }
                } else{
                    // Display an error message if username doesn't exist
                    $username_err = "No account found with that username.";
                }
            } else{
                echo "Oops! Something went wrong. Please try again later.";
            }

            // Close statement
            unset($stmt);
        }
    }
    
    // Close connection
    unset($pdo);
    file_put_contents( 'C:\temp\logs\debug' . time() . '.log', var_export( $username, true), FILE_APPEND);
//}
?>

    

    
    
<!--===============================================================================================-->
    <script src="vendor/jquery/jquery-3.2.1.min.js"></script>
<!--===============================================================================================-->
    <script src="vendor/animsition/js/animsition.min.js"></script>
<!--===============================================================================================-->
    <script src="vendor/bootstrap/js/popper.js"></script>
    <script src="vendor/bootstrap/js/bootstrap.min.js"></script>
<!--===============================================================================================-->
    <script src="vendor/select2/select2.min.js"></script>
<!--===============================================================================================-->
    <script src="vendor/daterangepicker/moment.min.js"></script>
    <script src="vendor/daterangepicker/daterangepicker.js"></script>
<!--===============================================================================================-->
    <script src="vendor/countdowntime/countdowntime.js"></script>
<!--===============================================================================================-->
    <script src="js/main.js"></script>

</body>

</html>

 

No, for a couple reasons:

1. Regular assignment (=) assigns values. $username gets the value of $confirm_password - it's not literally saying "$username is the same thing as $confirm_password".
2. Technically, $a = $b = $c does not say $b=$c; $a=$b but actually $b=$c; $a=$c. It's a minor point that basically never matters. So the statement is really more like $confirm_password = ""; $username = "".

Sorry. 

 

Notice: Undefined index: userid in C:\inetpub\wwwroot\Reports\login.php on line 162
Notice: Undefined index: password in C:\inetpub\wwwroot\Reports\login.php on line 171

 

 

When I add userid and password it checks users table if valid then should redirect to dashboard.php


 

 

 

Edited by AmbientGuitar

$_POST["userid"] and $_POST["password"] will only exist after the form has been submitted. The first time you load the page, obviously it won't have been, but that code there is still trying to run.

That thing you commented out?

//if($_SERVER["REQUEST_METHOD"] == "POST"){

That detects whether the form was submitted. You need it.

Sorry, I have uncommented and it has removed the errors. The console is showing nothing but redirection isn't working. How do you debug php. I am only doing this s few weeks!

Edited by AmbientGuitar
5 minutes ago, AmbientGuitar said:

but redirection isn't working.

terminate the script after a header redirect:

// Redirect user to welcome page perhaps
header('location: dashboard.php');
exit;

 

look closer at your code, please:
 

<span class="login100-form-title p-b-43" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" method="post">

it seems as though you may be new to html as well.
action and method attributes belong with the form element not the span element.

also, just use action="#" or leave it blank action="" versus the awful idea of PHP_SELF.

you should recode your form correctly.
you should also read about get and post methods.
your php script should be separated into post and get content:

if server request method = post then check login etc redirect to dashboard else show your form page

do you not understand the problems with your code?
try reading about forms and handling form submissions (posts).

remember to exit after a header redirect.

Best wishes.

  • Like 1

Also note that PHP errors do not show up in your browser's developer console*. They will be outputted as messages right into the page, or they'll show up in a log file, or they'll not show up at all, depending on how your PHP is set up.

It's confusing to me to need to say this because you clearly saw the Undefined Index notices earlier. Are you using some PHP extension to get errors into the console? Does it support doing that with fatal errors too?

(* edit: unless you have one of those PHP+browser extensions that lets you do that, but those are rarely used anymore)

Thanks guys! Making progress. I think its a PDO param that's causing this. However, jodunno was correct. My HTML i snot as it used to be. Thanks. I will get back when i have a solution. I have made progress.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.