Toy Posted January 26, 2011 Share Posted January 26, 2011 I'm doing something for the fun of it but I've kinda reached a problem! I have the login and index page on the same...eh, page, so I log in using the "login form" and it works fine, but I want it so that after i login the login form dissapears, I tried assigning a cookie to fix this but I can't go that way so yeah, help me! Quote Link to comment https://forums.phpfreaks.com/topic/225787-yo/ Share on other sites More sharing options...
Maq Posted January 26, 2011 Share Posted January 26, 2011 Can you post some relevant code? Quote Link to comment https://forums.phpfreaks.com/topic/225787-yo/#findComment-1165653 Share on other sites More sharing options...
Toy Posted January 26, 2011 Author Share Posted January 26, 2011 <?php if($_SERVER["REQUEST_METHOD"] == "POST") { $username=mysql_real_escape_string($_POST['username']); $password=mysql_real_escape_string($_POST['password']); $password=md5($password); $sql="SELECT xx FROM xx WHERE username='$username' and password='$password'"; $result=mysql_query($sql); $count=mysql_num_rows($result); if($count==1) { echo 'This part is only visible if logged in!'; } else { echo 'Sorry! The details you provided were incorrect, please try again...'; } } ?> <form action='<?php echo $_SERVER['PHP_SELF']; ?>' method='post'> Username: <input type='text' name='username' class='input'> Password: <input type='password' name='password' class='input'> <input type='submit' value='Login' class='input'> </form> Quote Link to comment https://forums.phpfreaks.com/topic/225787-yo/#findComment-1165656 Share on other sites More sharing options...
jaikob Posted January 26, 2011 Share Posted January 26, 2011 <?php session_start(); if(!isset($_SESSION['logged_in'])) { $_SESSION['logged_in'] = false; } if($_SESSION['logged_in'] == false) { if($_SERVER["REQUEST_METHOD"] == "POST") { $username=mysql_real_escape_string($_POST['username']); $password=mysql_real_escape_string($_POST['password']); $password=md5($password); $sql="SELECT xx FROM xx WHERE username='$username' and password='$password'"; $result=mysql_query($sql); $count=mysql_num_rows($result); if($count==1) { $_SESSION['logged_in'] = true; header("Location: ".$_SERVER['PHP_SELF']); } else { echo 'Sorry! The details you provided were incorrect, please try again...'; } } } else { // Use it here instead echo 'This part is only visible if logged in!'; } ?> <?php if($_SESSION['logged_in'] == false) { ?> <form action='<?php echo $_SERVER['PHP_SELF']; ?>' method='post'> Username: <input type='text' name='username' class='input'> Password: <input type='password' name='password' class='input'> <input type='submit' value='Login' class='input'> </form> <?php } ?> In theory the above will work. It may need a little modification as I have not tested it. Quote Link to comment https://forums.phpfreaks.com/topic/225787-yo/#findComment-1165657 Share on other sites More sharing options...
Toy Posted January 26, 2011 Author Share Posted January 26, 2011 <?php session_start(); if(!isset($_SESSION['logged_in'])) { $_SESSION['logged_in'] = false; } if($_SESSION['logged_in'] == false) { if($_SERVER["REQUEST_METHOD"] == "POST") { $username=mysql_real_escape_string($_POST['username']); $password=mysql_real_escape_string($_POST['password']); $password=md5($password); $sql="SELECT xx FROM xx WHERE username='$username' and password='$password'"; $result=mysql_query($sql); $count=mysql_num_rows($result); if($count==1) { $_SESSION['logged_in'] = true; header("Location: ".$_SERVER['PHP_SELF']); } else { echo 'Sorry! The details you provided were incorrect, please try again...'; } } } else { // Use it here instead echo 'This part is only visible if logged in!'; } ?> <?php if($_SESSION['logged_in'] == false) { ?> <form action='<?php echo $_SERVER['PHP_SELF']; ?>' method='post'> Username: <input type='text' name='username' class='input'> Password: <input type='password' name='password' class='input'> <input type='submit' value='Login' class='input'> </form> <?php } ?> In theory the above will work. It may need a little modification as I have not tested it. Thank you! I now get a "Warning: Cannot modify header information - headers already sent by (output started at C:\xampp\htdocs" on line: header("Location: ".$_SERVER['PHP_SELF']); very tired right now, sry if I'm bothering you with something stupid Quote Link to comment https://forums.phpfreaks.com/topic/225787-yo/#findComment-1165663 Share on other sites More sharing options...
BlueSkyIS Posted January 26, 2011 Share Posted January 26, 2011 is there really a blank line or ANYTHING before <?php if so, there can't be. Quote Link to comment https://forums.phpfreaks.com/topic/225787-yo/#findComment-1165673 Share on other sites More sharing options...
Pikachu2000 Posted January 26, 2011 Share Posted January 26, 2011 It appears that you haven't posted the complete code, and you haven't posted the complete error message. That makes it more difficult for others to actually help you. Quote Link to comment https://forums.phpfreaks.com/topic/225787-yo/#findComment-1165674 Share on other sites More sharing options...
Toy Posted January 26, 2011 Author Share Posted January 26, 2011 It's okay! I got it :-) that's all for now I hope. Quote Link to comment https://forums.phpfreaks.com/topic/225787-yo/#findComment-1165676 Share on other sites More sharing options...
Toy Posted January 27, 2011 Author Share Posted January 27, 2011 one last thing, I'm having kind of a problem with echoing out the username or whatever value of the logged in user, I don't really know why, could someone provide a working snippet of code that does this, I'd be forever grateful? Quote Link to comment https://forums.phpfreaks.com/topic/225787-yo/#findComment-1166123 Share on other sites More sharing options...
Maq Posted January 27, 2011 Share Posted January 27, 2011 You already have the value, why can't you just output it? Quote Link to comment https://forums.phpfreaks.com/topic/225787-yo/#findComment-1166129 Share on other sites More sharing options...
Toy Posted January 27, 2011 Author Share Posted January 27, 2011 Look, I don't really know! Every way I've tried just doesn't seem to work. Quote Link to comment https://forums.phpfreaks.com/topic/225787-yo/#findComment-1166140 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.