Jump to content

functions and sessions


tomfmason

Recommended Posts

I am starting to put alot of my backend processing into functions. So now I have a login script that I am wondering if I should start the session in the login script. Here we go.

Here is a section of the process.php

[code=php:0]
function showProcess($process) {
    switch ($process) {
  case "login":
                    include("functions.php");
        array_pop($_POST);
        if (get_magic_quotes_gpc()) {
        $_POST= array_map('stripslashes', $_POST);
        }
        $username = mysql_real_escape_string(trim($_POST['username']));
        $password = mysql_real_escape_string(trim($_POST['password']));
        $mdpwd = md5($password);
 
        $login = checkUser();
 
        if ($login == "true") {
            echo "You are now loged in";
        }else{
            echo "$login";
        }
  break;[/code]


and here is the checkUser function

[code=php:0]
function checkUser() {
  include("db.php");
  global $username;
  global $mdpwd;
 
  $sql = sprintf("SELECT COUNT(*) AS `login_match` FROM `users` WHERE `username` = '%s' AND `password` ='%s'", $username, $mdpwd);
  $res = mysql_query($sql) or die(mysql_error());
  $login_match = mysql_result($res, 0, 'login_match');
 
  if ($login_match == 1) {
      $q = mysql_query("SELECT * FROM `users` WHERE `username` ='$username'") or die(mysql_error());
 
      while ($rw = mysql_fetch_assoc($q)) {
            // I will set my session variables
            $_SESSION['whatever'] = $rw['whatever'];
        }
        $result = "true";
  }else{
        $result = "Your username and password do not match";
  }
  return $result;
}[/code]


My question is this a good way of doing this or ? and where should I place the session start? Should it be in the function or at the begining of the functions.php. 

Thanks,
Tom              
Link to comment
https://forums.phpfreaks.com/topic/16758-functions-and-sessions/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.