TheJoey Posted September 13, 2009 Share Posted September 13, 2009 Ive been trying to do this but it just doesnt show up the same as it does when i do it in html. <?php session_start(); ?> <html> <head> <link rel="stylesheet" href="assets/css/style.css" type="text/css" media="all" /> </head> <body> <?php $username = $_POST['username']; $password = $_POST['password']; $lines = file("users.txt",FILE_IGNORE_NEW_LINES); // read the lines into an array $find = "$username:$password"; // form a string like you expect it to be in the array if(in_array($find,$lines)){ $_SESSION['loginsuccessfull'] = true; echo echo '<div id="wrapper"> <div id="header"> <p class="logotext">NEw Stop <strong>Shop</strong><br> <span class="logotext2">Blah blah blah</span></p> <div id="nav"> <ul> <li><a href="">Site Map</a></li> <li><a href="">Privacy Policy</a></li> <li><a href="">Products</a></li> <li><a href="">Registration</a></li> <li><a href="index.html" class="on">Home</a></li> </ul> </div> </div> <div id="content"> <div id="left"> <h2>Addition Links</h2> <ul> <li><a href="">WHOOP WHOPP</a></li> <li><a href="">LOCATE US</a></li> <li><a href="">= Us</a></li> <li><a href="">2Us</a></li> </li> </ul> </div> <div id="breadcrumb"><strong>Home</strong></div> <div id="right"> <h1>NEW STOP SHOP</h1> <p>Login successfull</p> </div>'; } else { echo '<div id="wrapper"> <div id="header"> <p class="logotext">NEw Stop <strong>Shop</strong><br> <span class="logotext2">Blah blah blah</span></p> <div id="nav"> <ul> <li><a href="">Site Map</a></li> <li><a href="">Privacy Policy</a></li> <li><a href="">Products</a></li> <li><a href="">Registration</a></li> <li><a href="index.html" class="on">Home</a></li> </ul> </div> </div> <div id="content"> <div id="left"> <h2>Addition Links</h2> <ul> <li><a href="">WHOOP WHOPP</a></li> <li><a href="">LOCATE US</a></li> <li><a href="">= Us</a></li> <li><a href="">2Us</a></li> </li> </ul> </div> <div id="breadcrumb"><strong>Home</strong></div> <div id="right"> <h1>NEW STOP SHOP</h1> <p>Login Unsuccesfull</p> </div>'; } ?> </body> </html> Also when i click to go home, it gives me echo's "login unsuccessfull" before sending me back to home. Quote Link to comment Share on other sites More sharing options...
cbolson Posted September 13, 2009 Share Posted September 13, 2009 Hi, when you say "it just doesnt show up the same as it does when i do it in html." what do you mean? I can't see any css in your code that is related to the php. Not sure if it is your problem, but this line has a double "echo": echo echo '<div id="wrapper"> Also, unlesss there is more code than you have posted here, you are not actually saving the valid users logged in session so I am not surprised that when you click on a link it says "login unsuccessful" Sorry to not be of more help, it is just that I can't see exactly what (which) your problem is. Chris Quote Link to comment Share on other sites More sharing options...
TheJoey Posted September 13, 2009 Author Share Posted September 13, 2009 It is using a session, or so i thought. Well im currently not at home, the css works fine in html. That double echo may be a problem ill have a look. How may i fix the loggin? Quote Link to comment Share on other sites More sharing options...
ngreenwood6 Posted September 13, 2009 Share Posted September 13, 2009 you have to include the following code on every page that you want the session passed onto: <?php session_start(); ?> So if that is not on the home page it will say that because the session isnt present anymore. Quote Link to comment Share on other sites More sharing options...
cbolson Posted September 13, 2009 Share Posted September 13, 2009 Hi, Well im currently not at home, the css works fine in html. Well, as I say, I see no css-php related isssues here. So what page is this ? Is it the page after a login attempt? It is true that these lines: $username = $_POST['username']; $password = $_POST['password']; $lines = file("users.txt",FILE_IGNORE_NEW_LINES); // read the lines into an array $find = "$username:$password"; // form a string like you expect it to be in the array if(in_array($find,$lines)){ $_SESSION['loginsuccessfull'] = true; are setting a logged in $_SESSION. However, in the code that you have shown this only works if the values "username" and "password" are "posted" to the page. This normally only happens when the login form has been posted, not on every page. Ideally the login for would go to an "invisible" page that did nothing more than check the login values, if correct it would set the session variable as you have here, then redirect, using php header(), to the (eg) home page. If incorrect it would go somewhere else, cleary not defining the session. Then, on all pages that require the session variable you would check for that something like: session_start(); // check user is logged in - if not redirect if(!$_SESSION["loginsuccessfull"]) header("location: url_to_other_page"); // user logged in - show page..... Chris Quote Link to comment Share on other sites More sharing options...
TheJoey Posted September 13, 2009 Author Share Posted September 13, 2009 Your answer seems like a solution ill give it a go thanks. ill keep you posted Quote Link to comment Share on other sites More sharing options...
TheJoey Posted September 13, 2009 Author Share Posted September 13, 2009 im unsure where i put my redirect. So i play it on the same page as my login script correct? Because when i try do that it doesnt allow me to use else <?php session_start(); $username = $_POST['username']; $password = $_POST['password']; $lines = file("users.txt",FILE_IGNORE_NEW_LINES); // read the lines into an array $find = "$username:$password"; // form a string like you expect it to be in the array if(in_array($find,$lines)){ $_SESSION['loginsuccessfull'] = true; { if(!$_SESSION["loginsuccessfull"]) header("location: loginsuccess.php"); } else { $_SESSION["loginsuccessfull"] header("location: loginunsuccess.php"); } ?> ive never used a header before sorry Quote Link to comment Share on other sites More sharing options...
cbolson Posted September 13, 2009 Share Posted September 13, 2009 Nearly there.... Try this: <?php session_start(); $username = $_POST['username']; $password = $_POST['password']; $lines = file("users.txt",FILE_IGNORE_NEW_LINES); // read the lines into an array $find = "$username:$password"; // form a string like you expect it to be in the array if(in_array($find,$lines)){ $_SESSION['loginsuccessfull'] = true; # define session var header("location: loginsuccess.php"); # redirect to success page } else { header("location: loginunsuccess.php"); } ?> Then, as TheJoey says, don't forget to start every page with session_start(); Chris Quote Link to comment 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.