Checking a user is logged in

  • Mr. Wiggles
  • Graduate
  • Graduate
  • Mr. Wiggles
  • Posts: 160
  • Loc: Brizzle

Post 3+ Months Ago

Ok, in my previous topic I learned how to set up a login form.
Now when the user is sent to the next page the page should check if the user is logged in, but it doesn't check. The previous page shoudl ahve alos registered the users name. I want to be able to access the users name as logn as they are logged in so I can use it when they use other functions, such as automatically putting their name into a post.
This is the admin.php page.
PHP Code: [ Select ]
 
<?php
 
 
 
session_register('auth');
 
session_register('logname');
 
$doAction    = (empty($_GET['do'])) ? "": $_GET['do'];
 
$username    = (empty($_POST['username'])) ? "" : $_POST['username'];
 
$password    = (empty($_POST['password'])) ? "" : $_POST['password'];
 
if ($doAction != "" && $doAction == "login") {
 
  $connection     = mysql_connect("localhost","","")     or die ("Couldn't connect to the server <br>");
 
  $db             = mysql_select_db("",$connection)    or die ("Couln't find database <br>");
 
  $query         = "SELECT user from users WHERE user='$username'";
 
  $result         = mysql_query($query)                                or die ("Couldn't execute user query <br>");
 
  $num             = mysql_num_rows($result);
 
 
 
  if ($num > 0){
 
     $query     = "SELECT user FROM users WHERE user='$username' AND password='$password'";
 
     $result2     = mysql_query($query)                                 or die("Couldn't execute password query");
 
     $num2         = mysql_num_rows($result2);
 
     if ($num2 > 0){
 
         $auth        = "yes";
 
         $logname    = $username;
 
         $query     = "INSERT INTO login (login_user,login_time) VALUES ('$logname',NOW())";
 
         mysql_query($query)                                         or die("Couldn't execute login query");
 
         header("Location: <!-- m --><span class="postlink">http://animyst.gracious.me.uk/admin_main.php</span><!-- m -->");
 
     }else{
 
         $message = "You entered the wrong password<br>";
 
         include("login_form.inc");
 
     }
 
  }elseif ($num < 1){
 
   $message =  "There is no such user<br>";
 
   include("login_form.inc");
 
  }
 
}else{
 
    include("login_form.inc");
 
}
 
 
 
 
 
?>
 
 
  1.  
  2. <?php
  3.  
  4.  
  5.  
  6. session_register('auth');
  7.  
  8. session_register('logname');
  9.  
  10. $doAction    = (empty($_GET['do'])) ? "": $_GET['do'];
  11.  
  12. $username    = (empty($_POST['username'])) ? "" : $_POST['username'];
  13.  
  14. $password    = (empty($_POST['password'])) ? "" : $_POST['password'];
  15.  
  16. if ($doAction != "" && $doAction == "login") {
  17.  
  18.   $connection     = mysql_connect("localhost","","")     or die ("Couldn't connect to the server <br>");
  19.  
  20.   $db             = mysql_select_db("",$connection)    or die ("Couln't find database <br>");
  21.  
  22.   $query         = "SELECT user from users WHERE user='$username'";
  23.  
  24.   $result         = mysql_query($query)                                or die ("Couldn't execute user query <br>");
  25.  
  26.   $num             = mysql_num_rows($result);
  27.  
  28.  
  29.  
  30.   if ($num > 0){
  31.  
  32.      $query     = "SELECT user FROM users WHERE user='$username' AND password='$password'";
  33.  
  34.      $result2     = mysql_query($query)                                 or die("Couldn't execute password query");
  35.  
  36.      $num2         = mysql_num_rows($result2);
  37.  
  38.      if ($num2 > 0){
  39.  
  40.          $auth        = "yes";
  41.  
  42.          $logname    = $username;
  43.  
  44.          $query     = "INSERT INTO login (login_user,login_time) VALUES ('$logname',NOW())";
  45.  
  46.          mysql_query($query)                                         or die("Couldn't execute login query");
  47.  
  48.          header("Location: <!-- m --><span class="postlink">http://animyst.gracious.me.uk/admin_main.php</span><!-- m -->");
  49.  
  50.      }else{
  51.  
  52.          $message = "You entered the wrong password<br>";
  53.  
  54.          include("login_form.inc");
  55.  
  56.      }
  57.  
  58.   }elseif ($num < 1){
  59.  
  60.    $message =  "There is no such user<br>";
  61.  
  62.    include("login_form.inc");
  63.  
  64.   }
  65.  
  66. }else{
  67.  
  68.     include("login_form.inc");
  69.  
  70. }
  71.  
  72.  
  73.  
  74.  
  75.  
  76. ?>
  77.  
  78.  

It logs in fine. The php I was going to sue to check if the user is logged in is as follows.
PHP Code: [ Select ]
 
<?php
 
  session_start();
 
  if ($auth != "yes")
 
     {
 
     header("Location: <!-- m --><span class="postlink">http://animyst.gracious.me.uk/admin.php</span><!-- m -->");
 
     exit();
 
     }
 
include ("admin_main.inc");
 
?>
 
 
  1.  
  2. <?php
  3.  
  4.   session_start();
  5.  
  6.   if ($auth != "yes")
  7.  
  8.      {
  9.  
  10.      header("Location: <!-- m --><span class="postlink">http://animyst.gracious.me.uk/admin.php</span><!-- m -->");
  11.  
  12.      exit();
  13.  
  14.      }
  15.  
  16. include ("admin_main.inc");
  17.  
  18. ?>
  19.  
  20.  

It doesn't work and I need it to work and be able to remember the nbame of the suer aswell. It shoudlalready be stored in a session variable called logname. Anyone?
  • Mr. Wiggles
  • Graduate
  • Graduate
  • Mr. Wiggles
  • Posts: 160
  • Loc: Brizzle

Post 3+ Months Ago

Pleas help!
  • Rabid Dog
  • Web Master
  • Web Master
  • User avatar
  • Posts: 3245
  • Loc: South Africa

Post 3+ Months Ago

At the top of the page where you are calling session variables you need to have a call to session_start.
  • Mr. Wiggles
  • Graduate
  • Graduate
  • Mr. Wiggles
  • Posts: 160
  • Loc: Brizzle

Post 3+ Months Ago

Rabid Dog wrote:
At the top of the page where you are calling session variables you need to have a call to session_start.

Thanks. I tohugt i would need that. But that was the thing you said i didn't need before, ;)
  • Rabid Dog
  • Web Master
  • Web Master
  • User avatar
  • Posts: 3245
  • Loc: South Africa

Post 3+ Months Ago

If I recall correctly you weren't accessing session variables.

The only time you need to use it is if you are accessing session variables or session functions
  • Mr. Wiggles
  • Graduate
  • Graduate
  • Mr. Wiggles
  • Posts: 160
  • Loc: Brizzle

Post 3+ Months Ago

It registers hte variables, how can I access them using this same php?
  • Mr. Wiggles
  • Graduate
  • Graduate
  • Mr. Wiggles
  • Posts: 160
  • Loc: Brizzle

Post 3+ Months Ago

Also. If I want to have very long text in a mysql database, what would I use for it? blob?
  • Rabid Dog
  • Web Master
  • Web Master
  • User avatar
  • Posts: 3245
  • Loc: South Africa

Post 3+ Months Ago

PHP Code: [ Select ]
 
session_register('auth');
 
session_register('logname');
 
//variable names = $auth and $logname
 
//so effectively you could use them like this
 
echo $auth;
 
echo $logname;
 
 
  1.  
  2. session_register('auth');
  3.  
  4. session_register('logname');
  5.  
  6. //variable names = $auth and $logname
  7.  
  8. //so effectively you could use them like this
  9.  
  10. echo $auth;
  11.  
  12. echo $logname;
  13.  
  14.  


For long text strings I usually use the text type
  • Mr. Wiggles
  • Graduate
  • Graduate
  • Mr. Wiggles
  • Posts: 160
  • Loc: Brizzle

Post 3+ Months Ago

Will these variables be kept on until the user logs out?
Check my second program to see what I eman. So far you can access the page without auth
  • Mr. Wiggles
  • Graduate
  • Graduate
  • Mr. Wiggles
  • Posts: 160
  • Loc: Brizzle

Post 3+ Months Ago

I can't echo the variables in an inc file either. Help?!
  • Rabid Dog
  • Web Master
  • Web Master
  • User avatar
  • Posts: 3245
  • Loc: South Africa

Post 3+ Months Ago

try using var_dump(variable name here) to see what is happening
  • Mr. Wiggles
  • Graduate
  • Graduate
  • Mr. Wiggles
  • Posts: 160
  • Loc: Brizzle

Post 3+ Months Ago

string(3) "yes" string(4) "Hero"
Is what i'm getting. This shows that I am logged in and the suername. But when i use
PHP Code: [ Select ]
 
<?php
 
  echo $user;
 
?>
 
 
  1.  
  2. <?php
  3.  
  4.   echo $user;
  5.  
  6. ?>
  7.  
  8.  

inside my .inc file it shows $user literally
Can I get your msn so this is easier? Or would you find an annoying php illiterate person too much?
  • Rabid Dog
  • Web Master
  • Web Master
  • User avatar
  • Posts: 3245
  • Loc: South Africa

Post 3+ Months Ago

AIM screenname is rabbiddog5150.

I am actually on my way home now so I will have to look at it another time.

The var_dump shows you that the values are contained inside the variable so you can use the variables for comparisons and stuff.

Don't wrap variables in single quotes, they won't print then. if you want to print the variable rather put them inside double quotes (")
  • Mr. Wiggles
  • Graduate
  • Graduate
  • Mr. Wiggles
  • Posts: 160
  • Loc: Brizzle

Post 3+ Months Ago

It will display the variables from the php file, but will not display it from the .inc fie whether the vars are without quotes, in singles or in doubles.
  • Rabid Dog
  • Web Master
  • Web Master
  • User avatar
  • Posts: 3245
  • Loc: South Africa

Post 3+ Months Ago

Set up your server to process inc files the same way as php files or just change the extention of the inc file to php.

the way I do it usually is filename.inc.php
  • Mr. Wiggles
  • Graduate
  • Graduate
  • Mr. Wiggles
  • Posts: 160
  • Loc: Brizzle

Post 3+ Months Ago

Renamed and changed all things like that. Still doesn't work. Strange, another include I have displays variables just fine.
  • Mr. Wiggles
  • Graduate
  • Graduate
  • Mr. Wiggles
  • Posts: 160
  • Loc: Brizzle

Post 3+ Months Ago

Also, to log out, shoudl I have a separate page including just the
PHP Code: [ Select ]
 
session_destroy();
 
unset($variablenames..);
 
tags?
  1.  
  2. session_destroy();
  3.  
  4. unset($variablenames..);
  5.  
  6. tags?
  • Rabid Dog
  • Web Master
  • Web Master
  • User avatar
  • Posts: 3245
  • Loc: South Africa

Post 3+ Months Ago

Hmmm i think so, Haven' t really worked with these functions, like I said I work with reg_globals = off
session_destroy is a must but I think that destroying the session will unset any vars, might be wrong
  • Mr. Wiggles
  • Graduate
  • Graduate
  • Mr. Wiggles
  • Posts: 160
  • Loc: Brizzle

Post 3+ Months Ago

This is really *plum*. The variables are stored in the page. I can echo them through the .php file but when I include a file it won't display the variables. Why the hell not? Same code and all. Someone has to know!
  • Mr. Wiggles
  • Graduate
  • Graduate
  • Mr. Wiggles
  • Posts: 160
  • Loc: Brizzle

Post 3+ Months Ago

BTW I got the login to work. I realised when checking a variable like this :-
PHP Code: [ Select ]
 
if ($monkey = "yes"){
 
 
  1.  
  2. if ($monkey = "yes"){
  3.  
  4.  

You are actually not checking it, you are setting the variable to yes.
You need to use == to check.
  • Mr. Wiggles
  • Graduate
  • Graduate
  • Mr. Wiggles
  • Posts: 160
  • Loc: Brizzle

Post 3+ Months Ago

Ok, Got it to work. You can only echo or recall variables when they are echoed within a paragraph tag.
Now I need help with logging out. When I click the logout button it destroys the session and unregisters the variables, but when I check, you can still log in if you havent closed your browser. Lame.
Now I wanna know how I can have a user enter text into a form and have it outputted in a seperate form just as they entered it. So there should be correct line spacing and new paragraphs when they have hit enter. Anyone?

Post Information

  • Total Posts in this topic: 21 posts
  • Users browsing this forum: No registered users and 32 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.