简体   繁体   中英

PHP - How to fix “strpos” incorrectly evaluating my URL

I'm trying to create a dynamic error for invalid login credentials using php strpos. I can't seem get my strpos condition to run correctly.

I've tried using various conditional arguments including returning anything greater than 0. [No effect]

$fullUrl returns: http://localhost/pages/login.php?signin=error

if(strpos($fullUrl, "login.php?signin=error" > 0))

if(strpos($fullUrl, "login.php?signin=error" !== false))

if(strpos($fullUrl, "login.php?signin=error" == true))

When I use the "!strpos" it will show my first echo but will not update when the URL changes.

if(!strpos($fullUrl, "pages/login.php?signin=error" == true))
<div id="page-container">
          <div id="login">
            <div id="login-panel">
            <p style="padding: 0px; margin: 0px;">Member Login</p>
            <div id="login-error">
              <form action="../includes/login_handler.php" autocomplete="off" method="post">
                <?php
                $fullUrl = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
                  if(strpos($fullUrl, "login.php?signin=error" !== false)) {
                    echo '<p style=" color: red; font-size: 15px; visibility: visible;"> Invalid Login Credentials </p>';
                  } else
                    echo '<p style="font-size: 15px; visibility: hidden;"> Invalid Login Credentials </p>';
                ?>

                <input font-size: 16px" placeholder="Username" maxlength="40" type="text"  id="username" name="username"/> <br/>
                <input font-size: 16px" placeholder="Password" type="password" id="password" name="password"/> </br>
                <input id="login_button" action="javascript:document.location.reload()" type="submit" maxlength="50" value="Login" name="Login"/>

                <div id="remember_me_checkbox">
                <input type="checkbox" name="remember" value="checked">
                <l for="rememberme">Remember Me</l>
                </div>
                <div id="forgot_password"><div><a><i>Forgot Password?</i></a></div></div>
              </form>
            </div>
          </div>

Only issue is strpos is not correctly finding "login.php?signin=error" at the end of my URL.

You should check result of strpos function.

if(strpos($fullUrl, "login.php?signin=error") !== false)
    .....
if(strpos($fullUrl, "login.php?signin=error") > -1)
    .....

read document first: https://www.php.net/manual/en/function.strpos.php

Also, You can check the GET parameter

if (isset($_GET['signin']) && $_GET['signin'] == 'error')
    echo 'ERROR';

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM