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