Jump to content

[SOLVED] session login :(((


asmith

Recommended Posts

my login page with session don't work !! :(

 

login:

.

.

.

if ($num_rows != 0)

{$auth="<a href=\"hostgame.php\">host a game!</a>";

session_start();

session_register('abc');

$_SESSION[abc]="bbb";

 

}

else

{$auth="bad login";}

?>

<html>

<head>

</head>

<body>

<?php

echo $auth;

?>

</body

</html>

 

 

a.php  :

<?php

if ($_SESSION[abc] == "bbb")

{$msg="hello !";}

else

{header("location: /my2/login.html");

exit;

}

?>

<html>

.

.

.

 

it shows login page again ! please help !! :(

 

 

Link to comment
Share on other sites

try using single quotes for echo and variable statements like

$auth="<a href=\"hostgame.php\">host a game![/url]";

it avoids needing to add backslashes into the HTML.

 

Also laying out the code a little better might help spot errors...

 

Regarding your problem:

 

what I do is open the session at the top of the page, check the posted details against the database to see if a record exists, count the number of rows, if the number of rows >=1 then log the user in by setting a couple of sessions (normally a session called "Logged_In" and one called "my_id" (to act as a short-cut to find out the database id number of the user) before using a header redirect to the user's control panel.

 

the control panel (and all subsequent member-only pages) also have a session open statement, followed by a session "Logged_In" member check with a redirect for anyone who has not registered the logged in session.

 

It tends to work very well for me.

 

This also allows the login page itself to have all the login code at the top of the page, followed by error checks and appropriate messages, in case the redirect script was not triggered.

 

I hope this helps... but it may involve having to write all of it again. (Sorry)

Link to comment
Share on other sites

i understand what you said , could you please give some example codes ?

and some lines i wrote before is wrong , here is the original on my pc  :

 

login:

.

.

.

if ($num_rows != 0)

{$auth="<a href=\"a.php\">host a game!</a>";

session_start();

session_register('abc');

$_SESSION[abc]="bbb";

 

}

else

{$auth="bad login";}

?>

<html>

<head>

</head>

<body>

<?php

echo $auth;

?>

</body

</html>

 

 

a.php  :

<?php

if ($_SESSION[abc] == "bbb")

{$msg="hello !";}

else

{header("location: /my2/login.html");

exit;

}

?>

<html>

.

.

.

 

 

 

Link to comment
Share on other sites

Can you please use [ code ][ /code ] tags (without the spaces) when posting code. Also, as I said before, session_register() has long been depricated.

 

I'm not sure how you logging users in as there doesn't appear to be any check, but I assume this is jus some sort of test code. You might want to post the rest of your code so we can see whats happening, also a description of your actual issue would help.

Link to comment
Share on other sites

ok , here the total codes :

 

<?php

 

$conn=mysql_connect("localhost","john","1234");

 

$db=mysql_select_db("testdb",$conn);

$sql="select * from users where username='$_POST[username]' and password='$_POST[password]'";

$sc=mysql_query($sql,$conn) or die(mysql_error());

$nums=mysql_num_rows($sc);

if ($nums != 0)

{$auth="[a href=\"a.php\"] a page![/a]";

session_start();

session_register('abc');

$_SESSION[abc]="bbb";

 

}

else

{$auth="bad login";}

?>

<html>

<head>

</head>

<body>

<?php

echo $auth;

?>

</body>

</html>

 

 

a.php  :

 

 

<?php

if ($_SESSION['abc'] == 'bbb')

{$msg="hello!";}

else

{header("location: /my2/login.html");

exit;

}

?>

 

<html>

<body>

<?php

echo $msg;

?>

</body>

</html>

 

note i see the "a page" link , but when i click on it i see the login page again .

thanks

Link to comment
Share on other sites

session_start(); --> Do this at the beginning of all your PHP pages. That's what gerkintrigg was saying.

It should solve your problem immediately.

 

:D it worked !!

IE works fine, but when i close the FF , and open again it shows the page again , seems FF do not end the session after the browser is closed, is that bug ?

what should i do to make it end when  ff closes too ? 

Link to comment
Share on other sites

As has been said, you need session_start() in the top of a.php. Also, as has been said, session_register(0 has long been depricated. Try..

 

<?php

 $conn = mysql_connect("localhost","john","1234");
 $db = mysql_select_db("testdb",$conn);
 $sql = "SELECT * FROM `users` WHERE `username` = '{$_POST['username']}' && `password`= '{$_POST['password']}'";
 if ($sc = mysql_query($sql,$conn)) {
   if (mysql_num_rows($sc)) {
     $auth = "<a href=\"a.php\"> a page!</a>";
     session_start();
     $_SESSION['abc'] = "bbb";
   } else {
     $auth = "bad login";
   }
 }

?>
<html>
<head>
</head>
<body>
 <?php echo $auth; ?>
</body>
</html>

 

a.php

<?php

 session_start();
 if (isset($_SESSION['abc']) && $_SESSION['abc'] == 'bbb') {
   $msg = "hello!";
 } else {
   header("location: /my2/login.html");
   exit;
 }

?>

<html>
<body>
 <?php echo $msg; ?>
</body>
</html>

Link to comment
Share on other sites

Something else to think about...

 

You can set the length of the session by using a persistent session. This length is from the end of the last logged-in request.

This will effectively log them off after xx minutes of inactivity.

Note that the length before expiry is user-side. It's up to their browser to comply with the length of the cookie.

 

<?php
$length = 3600; // length in seconds of session --> 3600 seconds = 1 hour
session_set_cookie_params($length, "/", ".yourdomain.com");
session_start();   //Start session

// Your PHP code here
?>

 

Be sure to replace yourdomain.com with your domain name.

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.