Sparrowhawk Posted August 21, 2011 Share Posted August 21, 2011 Hello all, I'm new to any kind of coding, but I recently graduated college with an English degree and decided to try to become a technical writer, but quickly realized I would need to learn some basic coding to publish stuff as many companies are moving away from paper. Anyway, that's the background to why I'm about to ask a really simple question: I wrote a login script, and it rejects people correctly who don't have the correct credentials, and it accepts the people that do, but while the failed attempts send the appropriate alert (the alert also pops up when u first open the page, which if you all can figure out why and stop it, that would be awesome, but its not the main problem), for those who do log in it does not send them to the link that I'm trying to send them to upon log in. Instead, they stay on the same page, but the login text boxes are gone. Any help would be appreciated, thanks. Here is the code: <html> <body style="background-color:rgb(240,230,200);font-family:Times;font-size:110%;"> <p style="text-align:center;font-family:Arial;font-size:250%"> ///text removed for privacy///</p> <p style="text-align:center;font-family:Arial;font-size:200%"> ////text removed for privacy </p> <CENTER><img src="////removed for privacy/////" width="700" height="500"/></Center> <p style="text-align:center;"> To access the site, please use enter your User ID and Password. </p> <center> <?php error_reporting (E_ALL ^ E_NOTICE); ?> <?php session_start(); include("passwords.php"); if ($_POST["ac"]=="log") { if ($USERS[$_POST["username"]]==$_POST["password"]) { $_SESSION["logged"]=$_POST["username"]; } else { echo 'Please enter a vaild Username and Password'; }; }; if (array_key_exists($_SESSION["logged"],$USERS)) header("index.html"); else { echo "<script>alert('Please enter valid Username and Password')</script>"; echo '<form action="login.php" method="post"><input type="hidden" name="ac" value="log"> '; echo 'Username: <input type="text" name="username" /><br />'; echo 'Password: <input type="password" name="password" /><br />'; echo '<input type="submit" value="Login" />'; echo '</form>'; } ?> </center> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/245378-login-link-not-working/ Share on other sites More sharing options...
voip03 Posted August 21, 2011 Share Posted August 21, 2011 1. please use code tag. 2. input submit button name is missing <input type="button" name="sc" value="Login"> 3. you neede if else . if you want the form to display. Quote Link to comment https://forums.phpfreaks.com/topic/245378-login-link-not-working/#findComment-1260302 Share on other sites More sharing options...
Sparrowhawk Posted August 21, 2011 Author Share Posted August 21, 2011 sorry, unsure what you mean by code tag, first post here, or anywhere regarding code. the buttons currently are fine as they are - when the page loads the first time, and when it rejects a user. however, instead of linking to the next page if their username and password are correct, it simply keeps them at the same page (even url is the same) but the input fields are gone. I want to make it where it goes to a whole other page when they login correctly. also, i'm not sure why the alert is appearing before they even attempt to login the first time, but as it's not affecting functionality, it's only a minor issue. Quote Link to comment https://forums.phpfreaks.com/topic/245378-login-link-not-working/#findComment-1260336 Share on other sites More sharing options...
litebearer Posted August 21, 2011 Share Posted August 21, 2011 when creating a post here, one of the 'buttons' is the # sign. If you click on it, there will be an opening and closing 'code tag'. By placing your code between these two tags it makes it easier for people to distinguish what part of your post is code and what part is explanation/question/etc Quote Link to comment https://forums.phpfreaks.com/topic/245378-login-link-not-working/#findComment-1260343 Share on other sites More sharing options...
Sparrowhawk Posted August 22, 2011 Author Share Posted August 22, 2011 Ok, then if this will help, here is the code again: <html> <body style="background-color:rgb(240,230,200);font-family:Times;font-size:110%;"> <p style="text-align:center;font-family:Arial;font-size:250%"> ///text removed for privacy///</p> <p style="text-align:center;font-family:Arial;font-size:200%"> ////text removed for privacy//// </p> <CENTER><img src="////removed for privacy/////" width="700" height="500"/></Center> <p style="text-align:center;"> To access the site, please use enter your User ID and Password. </p> <center> <?php error_reporting (E_ALL ^ E_NOTICE); ?> <?php session_start(); include("passwords.php"); if ($_POST["ac"]=="log") { if ($USERS[$_POST["username"]]==$_POST["password"]) { $_SESSION["logged"]=$_POST["username"]; } else { echo 'Please enter a vaild Username and Password'; }; }; if (array_key_exists($_SESSION["logged"],$USERS)) header("index.html"); else { echo "<script>alert('Please enter valid Username and Password')</script>"; echo '<form action="login.php" method="post"><input type="hidden" name="ac" value="log"> '; echo 'Username: <input type="text" name="username" /><br />'; echo 'Password: <input type="password" name="password" /><br />'; echo '<input type="submit" value="Login" />'; echo '</form>'; } ?> </center> </body> </html> What I really need to know is why the link in line 20 header("index.html"); is not working. Quote Link to comment https://forums.phpfreaks.com/topic/245378-login-link-not-working/#findComment-1260352 Share on other sites More sharing options...
KevinM1 Posted August 22, 2011 Share Posted August 22, 2011 You can't use header after output is sent to the browser. This means ANY output, including a simple space or return. See the following thread for more info: http://www.phpfreaks.com/forums/index.php?topic=37442.0 Quote Link to comment https://forums.phpfreaks.com/topic/245378-login-link-not-working/#findComment-1260364 Share on other sites More sharing options...
teynon Posted August 22, 2011 Share Posted August 22, 2011 While you should look at the link, the simple solution here is to do this: <?php error_reporting (E_ALL ^ E_NOTICE); session_start(); include("passwords.php"); if ($_POST["ac"]=="log") { if ($USERS[$_POST["username"]]==$_POST["password"]) { $_SESSION["logged"]=$_POST["username"]; } else { echo 'Please enter a vaild Username and Password'; }; }; if (array_key_exists($_SESSION["logged"],$USERS)) header("index.html"); } ?> <html> <body style="background-color:rgb(240,230,200);font-family:Times;font-size:110%;"> <p style="text-align:center;font-family:Arial;font-size:250%"> ///text removed for privacy///</p> <p style="text-align:center;font-family:Arial;font-size:200%"> ////text removed for privacy//// </p> <CENTER><img src="////removed for privacy/////" width="700" height="500"/></Center> <p style="text-align:center;"> To access the site, please use enter your User ID and Password. </p> <center> <?php // I wouldn't code this here, but i'm too lazy to take all your php off of it. echo "<script>alert('Please enter valid Username and Password')</script>"; echo '<form action="login.php" method="post"><input type="hidden" name="ac" value="log"> '; echo 'Username: <input type="text" name="username" /><br />'; echo 'Password: <input type="password" name="password" /><br />'; echo '<input type="submit" value="Login" />'; echo '</form>'; ?> </center> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/245378-login-link-not-working/#findComment-1260369 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.