-
Posts
10 -
Joined
-
Last visited
Everything posted by Mistral π€
-
Thanks so much! Works well.
-
Yes sorry I have actually fixed it, I deleted the following $username = isset($_POST['username']) ? $_POST['username'] : ''; $password = isset($_POST['password']) ? $_POST['password'] : ''; Then by doing so, I can run the header() again, as what it was doing was not running on localhost. Hope it makes sense. Thanks.
-
I have an issue trying to use header("location: admin.php"); on a login.php form. When finally finishing off all the username and password message I can not log in to the admin page, vier the the login. The login page starts with if ($_SERVER["REQUEST_METHOD"] == "POST") { then usual username and password. Changing something else to header("location: http://www.d__.com"); will work. It has something to do with location: ../admin.php or .admin.php I donβt knowβ¦. Hope this makes sense.
-
I am using POST and initially works well when using the Username and Password, after the next step I use a TXT file_get_contents to explode. The problem is the message that I don't get a response. It may have something to do with if($password == $passwordCompare) { <form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>"> <p>You can create your own password or generate a password, leave the password box empty to generate a password.</p> <div> <label>Username:</label> <input type="text" name="username" value="<?php echo $name;?>"> <span class="error">* <?php echo $usernameErr;?></span> </div> <br> <div> <label>Password:</label> <input type="password" name="password" value="<?php echo $password;?>"> <span class="error">* <?php echo $passwordErr;?></span> </div> <br> <input type="submit" name="submit" value="Submit"> </form> <?php $usernameErr = $passwordErr = $websiteErr = ""; $username = $password = $website = ""; if ($_SERVER["REQUEST_METHOD"] == "POST") { if (empty(trim($_POST["username"]))) { $usernameErr = "Username is required"; } else { $username = test_input($_POST["username"]); if (!preg_match("/^[a-zA-Z0-9]*$/", $username)) { $usernameErr = "Only letters and numbers allowed"; } } if (empty(trim($_POST["password"]))) { $passwordErr = "Password is required"; } else { $password = test_input($_POST["password"]); if (!preg_match("/^[a-zA-Z0-9]*$/", $password)) { $passwordErr = "Only letters and numbers allowed"; } } if ($username == true && $password == true) { $contents = file_get_contents("accounts.txt", true); $lines = explode("\n", $contents); $userfound = "N"; for ($i=0; $i < count($lines)-1; $i++) { $fields = explode(" ", $lines[$i]); if ($fields[0] == $username) { $passwordCompare = $fields[1]; $userfound = "Y"; } } if($password == $passwordCompare) { $success = "<p class=\"success\">The username is already exists</p>"; header("location: admin.php"); exit; } else { if ($userfound == "Y") { $passwordErr = "Username/Password incorrect!"; } else { $passwordErr = "Username/Password not found!"; } } } } function test_input($data) { $data = trim($data); $data = stripslashes($data); $data = htmlspecialchars($data); return $data; } ?> Example of POST AND TXT
-
Sorry i cannot figure this out, is there some example of this? As I also tried if(strlen($username) - substr_count($username, ' ')) { $username_error = ".."; }
-
I am using PHP TXT it seems to work well so far, however the username and password will run with a space, when entering two or more characters. For example: Create Account Username this will work with space need to one space Password ******** I have been trying to use preg_match with "/^[a-zA-Z0-9]*$/" but it just does not work on preg_match ($username) <?php $username = null; $password = null; $username_error = null; $password_error = null; $success = null; if(isset($_POST["sign-up"])) { $username = $_POST["username"]; $password = $_POST["password"]; if(empty(trim($username))) { if(preg_match("/^[a-zA-Z0-9]*$/", $username)) { $username = 0; $username_error = "<p><i>Username field has invalid content</i></p>"; } elseif(preg_match("/\\s/", $username)) { $username_error = "<p><i>Space found in username</i></p>"; } else { $username_error = "<p><i>Username field is empty</i></p>"; } } else { if(empty(trim($password))) { $password_error = "<p><i>Password field is empty</i></p>"; } else { $var = file("accounts.txt", FILE_SKIP_EMPTY_LINES|FILE_IGNORE_NEW_LINES); $exists = false; foreach($var as $key => $value) { $user = explode(' ', $value); if(trim($user[0]) == $username) { // we found a match this is enough to call it a duplicate $exists = true; break; } } if($exists) { $success = "<p class=\"success\">The username is already exists</p>"; } else { file_put_contents("accounts.txt", $username." ".$password.PHP_EOL, FILE_APPEND); $success = "<p class=\"success\">You are registered successfully</p>"; header("Location: login.php?status=success"); } } } } ?> <form method="POST" action="<?php echo $_SERVER["PHP_SELF"]; ?>"> <h4>Create Account</h4><br> <div> <label for="username">Username</label> <input type="text" name="username" value="<?php echo $_POST['username']; ?>"> <div class="error username-error"> <?php echo $username_error; ?> </div> </div> <div> <label>Password</label> <input type="password" name="password" value="<?php echo $_POST['password']; ?>"> <div class="error password-error"> <?php echo $password_error; ?> </div> </div> <p> <input type="submit" name="sign-up" value="Sign Up"> <div class="success"> <?php echo $success; ?> </div> </p> </form>
-
Thanks you so much Gurus, that worked well.
-
I'm on the mac console and don't get any errors
-
Yes I know. The main issue is not to use sql but just to use txt instead with fopen('accounts.txt', 'a+'), and that is the main issue of running the 3rd part. login.txt
-
I have been trying to create a registration and log-in page, the registration works well but the log-in page is having an issue. When submitting the first username and password work, but the issue happens on the "fopen('accounts.txt', 'a+');". <?php session_start(); session_unset(); // input fields validation if ($_SERVER["REQUEST_METHOD"] == "POST") { echo "Your username is " . $_POST['username'] . "<br>"; echo "You password is " . $_POST['password'] ; $passwordCompare = ""; $username = $_POST["username"]; $password = $_POST["password"]; if ($_POST['username'] == "" ) { // not defined if (empty($_POST['username'])) { $msg = "Username missing"; echo $msg . "Username missing"; } else { // check if name only contains letters and whitespace if (!preg_match("/^[a-zA-Z ]*$/", input_data($_POST['username']))) { $msg = "Only alphabets and white space are allowed"; echo $msg . "mathing this "; } echo $msg . "Username missing is just dumb"; } } elseif ($_POST['password'] == "") { // not defined if (empty($_POST['password'])) { $msg = "Pass missing"; } else { // check if name only contains letters and whitespace if (!preg_match("/^[a-zA-Z ]*$/", input_data($_POST['password']))) { $msg = "Only alphabets and white space are allowed"; } } } else { // THE MAIN ISSUE HERE $passwordCompare = ""; $username = $_POST["username"]; $password = $_POST["password"]; $myfile = fopen('accounts.txt', 'a+'); $contents = file_get_contents($myfile); $lines = explode("\n", $contents); $userfound = 'N'; for ($i=0; $i < count($lines)-1; $i++) { $fields = explode(" ", $lines[$i]); if ($fields[0] == $username) { $passwordCompare = $fields[1]; $userfound = 'Y'; } } if ($password == $passwordCompare) { // Redirect to the next webpage $_SESSION['uid'] = $username; header("location: admin.php"); exit; } else { if ($userfound == 'Y') { $msg = ' Password incorrect !'; } else { $msg = ' Username not found !'; } } } } // trim, stripslashes and htmlspecialchars function input_data($data) { $data = trim($data); $data = stripslashes($data); $data = htmlspecialchars($data); return $data; } ?> <form method="post" action="<?php echo $_SERVER["PHP_SELF"]; ?>"> <div> <label for="username">Username:</label> <input type="text" id="username" name="username"> </div> <div> <label for="password">Password:</label> <input type="password" id="password" name="password"> </div> <div> <input type="submit" name="submit" value="Login"> </div> </form>