Jump to content

[SOLVED] Logged In problem.


Cory94bailly

Recommended Posts

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.

Link to comment
Share on other sites

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;
?>

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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).

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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;
?>

 

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.