Cory94bailly Posted May 22, 2008 Share Posted May 22, 2008 Loggedin.php: <? //Connect to Members DB mysql_connect('p41mysql145.secureserver.net', 'cory_fcs', 'Sils1234') or die(mysql_error()); mysql_select_db('cory_fcs') or die(mysql_error()); if(isset($_COOKIE['ID_fcs_member'])) { //Look for the cookies $username1 = $_COOKIE['ID_fcs_member']; $pass1 = $_COOKIE['Key_fcs_member']; $username2 = $_COOKIE['ID_fcs_team']; $pass2 = $_COOKIE['Key_fcs_team']; $check1 = mysql_query("SELECT * FROM members WHERE username = '$username1'")or die(mysql_error()); $check2 = mysql_query("SELECT * FROM team WHERE username = '$username2'")or die(mysql_error()); if (mysql_num_rows($check1) > 0) { //They have a member's cookie! } elseif (mysql_num_rows($check2) > 0) { //The have a team cookie! { //If the cookie exists, show the "Logout" button. { ?> <a href="logout.php">Logout</a> <? } } } } else //If the cookie does not exist, show the "Login" button. { ?> <a href="login.php">Login</a> <img src="images/splitter.gif" class="splitter" alt="" /> <a href="register.php">Register</a> <? } ?> I have it included on my pages and it worked great with a single cookie but now it barely works.. Problem: When I am logged out, it shows the "Login" button fine but if I log in, it shows nothing. Quote Link to comment https://forums.phpfreaks.com/topic/106816-solved-logged-in-problem/ Share on other sites More sharing options...
947740 Posted May 22, 2008 Share Posted May 22, 2008 May I recommend sessions over cookies? They are much easier to use. (In my opinion.) Quote Link to comment https://forums.phpfreaks.com/topic/106816-solved-logged-in-problem/#findComment-547569 Share on other sites More sharing options...
jonsjava Posted May 22, 2008 Share Posted May 22, 2008 a different approach: <? //Connect to Members DB mysql_connect('p41mysql145.secureserver.net', 'cory_fcs', 'Sils1234') or die(mysql_error()); mysql_select_db('cory_fcs') or die(mysql_error()); if(isset($_COOKIE['ID_fcs_member'])) { //Look for the cookies $username1 = $_COOKIE['ID_fcs_member']; $pass1 = $_COOKIE['Key_fcs_member']; $username2 = $_COOKIE['ID_fcs_team']; $pass2 = $_COOKIE['Key_fcs_team']; $check1 = mysql_query("SELECT * FROM members WHERE username = '$username1'")or die(mysql_error()); $check2 = mysql_query("SELECT * FROM team WHERE username = '$username2'")or die(mysql_error()); if (mysql_num_rows($check1) > 0) { //They have a member's cookie! } elseif (mysql_num_rows($check2) > 0) { //The have a team cookie! $login_logout = "<a href='logout.php'>Logout</a>"; { //If the cookie exists, show the "Logout" button. { ?> <a href="logout.php">Logout</a> <? } } } } else //If the cookie does not exist, show the "Login" button. { $login_logout = "<a href='login.php'>Login</a> <img src='images/splitter.gif' class='splitter' alt='' /> <a href='register.php'>Register</a>"; ?> <? } print $login_logout; ?> Quote Link to comment https://forums.phpfreaks.com/topic/106816-solved-logged-in-problem/#findComment-547570 Share on other sites More sharing options...
imdead Posted May 22, 2008 Share Posted May 22, 2008 Yep Sessions Ftw Quote Link to comment https://forums.phpfreaks.com/topic/106816-solved-logged-in-problem/#findComment-547571 Share on other sites More sharing options...
Cory94bailly Posted May 22, 2008 Author Share Posted May 22, 2008 Woops.. just noticed that I left my mysql stuff in... Oh well.. I changed it this morning anyway xD May I recommend sessions over cookies? They are much easier to use. (In my opinion.) Sessions are domain specific (I heard) and it's a subdomain. Quote Link to comment https://forums.phpfreaks.com/topic/106816-solved-logged-in-problem/#findComment-547651 Share on other sites More sharing options...
Darklink Posted May 22, 2008 Share Posted May 22, 2008 If you want something basic, stick with sessions, otherwise to be honest I don't recommend sessions at all. Your code is a bit of a mess, so it's hard to understand. Why do you have opening brackets when there is no need for them in some areas? Quote Link to comment https://forums.phpfreaks.com/topic/106816-solved-logged-in-problem/#findComment-547658 Share on other sites More sharing options...
wildteen88 Posted May 22, 2008 Share Posted May 22, 2008 May I recommend sessions over cookies? They are much easier to use. (In my opinion.) Sessions are domain specific (I heard) and it's a subdomain. Sessions can work over subdomains. Make sure session.cookie_domain is set to .domain.com (NOTE: the . at the beginning is important). Quote Link to comment https://forums.phpfreaks.com/topic/106816-solved-logged-in-problem/#findComment-547659 Share on other sites More sharing options...
Cory94bailly Posted May 22, 2008 Author Share Posted May 22, 2008 I have no clue how to use sessions anyway.. Cookies seem easier to me.. lol Quote Link to comment https://forums.phpfreaks.com/topic/106816-solved-logged-in-problem/#findComment-547660 Share on other sites More sharing options...
Cory94bailly Posted May 22, 2008 Author Share Posted May 22, 2008 a different approach: <? //Connect to Members DB mysql_connect('p41mysql145.secureserver.net', 'cory_fcs', 'Sils1234') or die(mysql_error()); mysql_select_db('cory_fcs') or die(mysql_error()); if(isset($_COOKIE['ID_fcs_member'])) { //Look for the cookies $username1 = $_COOKIE['ID_fcs_member']; $pass1 = $_COOKIE['Key_fcs_member']; $username2 = $_COOKIE['ID_fcs_team']; $pass2 = $_COOKIE['Key_fcs_team']; $check1 = mysql_query("SELECT * FROM members WHERE username = '$username1'")or die(mysql_error()); $check2 = mysql_query("SELECT * FROM team WHERE username = '$username2'")or die(mysql_error()); if (mysql_num_rows($check1) > 0) { //They have a member's cookie! } elseif (mysql_num_rows($check2) > 0) { //The have a team cookie! $login_logout = "<a href='logout.php'>Logout</a>"; { //If the cookie exists, show the "Logout" button. { ?> <a href="logout.php">Logout</a> <? } } } } else //If the cookie does not exist, show the "Login" button. { $login_logout = "<a href='login.php'>Login</a> <img src='images/splitter.gif' class='splitter' alt='' /> <a href='register.php'>Register</a>"; ?> <? } print $login_logout; ?> Btw.. same thing, logged out works but if I log in.. it shows nothing. Quote Link to comment https://forums.phpfreaks.com/topic/106816-solved-logged-in-problem/#findComment-547662 Share on other sites More sharing options...
wildteen88 Posted May 22, 2008 Share Posted May 22, 2008 Well sessions use cookies but not in the same way. It uses a cookie to set what is known as the session identifier all session data is then stored server-side. Sessions are more secure than cookies. Example basic usage of sessions: <?php session_start(); // this must be called on ALL pages which use sessions // set a session variable $_SESSION['test'] = 'hello'; ?> <a href="sess_test.php">Retrive session data</a> <?php session_start(); echo $_SESSION['test']; ?> Run sess_init.php then click the link to sess_test.php Quote Link to comment https://forums.phpfreaks.com/topic/106816-solved-logged-in-problem/#findComment-547665 Share on other sites More sharing options...
Darklink Posted May 22, 2008 Share Posted May 22, 2008 Heres your code cleaned up. Does it work? <?php //Connect to Members DB mysql_connect('p41mysql145.secureserver.net', 'cory_fcs', 'Sils1234') or die(mysql_error()); mysql_select_db('cory_fcs') or die(mysql_error()); if(isset($_COOKIE['ID_fcs_member'])) { //Look for the cookies $username1 = $_COOKIE['ID_fcs_member']; $pass1 = $_COOKIE['Key_fcs_member']; $username2 = $_COOKIE['ID_fcs_team']; $pass2 = $_COOKIE['Key_fcs_team']; // Fetch data $check1 = mysql_query("SELECT * FROM members WHERE username = '$username1'")or die(mysql_error()); $check2 = mysql_query("SELECT * FROM team WHERE username = '$username2'")or die(mysql_error()); if (mysql_num_rows($check1) > 0) { //They have a member's cookie! $login_logout = "<a href='logout.php'>Logout</a>"; } elseif (mysql_num_rows($check2) > 0) { //The have a team cookie! $login_logout = "<a href='logout.php'>Logout</a>"; } } else { $login_logout = "<a href='login.php'>Login</a> <img src='images/splitter.gif' class='splitter' alt='' /> <a href='register.php'>Register</a>"; } print $login_logout; ?> Quote Link to comment https://forums.phpfreaks.com/topic/106816-solved-logged-in-problem/#findComment-547666 Share on other sites More sharing options...
Cory94bailly Posted May 22, 2008 Author Share Posted May 22, 2008 Heres your code cleaned up. Does it work? Yep, thanks! You guys here at phpfreaks are life savers!!! Btw, I'm considering sessions but they seem a bit complicated.. I mean like.. do I need 2 files for one script? Quote Link to comment https://forums.phpfreaks.com/topic/106816-solved-logged-in-problem/#findComment-547671 Share on other sites More sharing options...
Darklink Posted May 23, 2008 Share Posted May 23, 2008 Sessions are not complicated. At the top of each script you must place the session_start() function and then simply set the session variables. Bare in mind I haven't used session vars in a few years now, but it should be something like: <?php session_start(); // Always at the top of each page $_SESSION['example'] = 'test'; echo $_SESSION['example']; ?> The session variables will pass through pages BUT it will add annoyances like PHPSESSID vars in the URL and it's just simply basic working. Cookies are the best choice in my opinion. Quote Link to comment https://forums.phpfreaks.com/topic/106816-solved-logged-in-problem/#findComment-547946 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.