Jump to content

Archived

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

tomfmason

functions and sessions

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              

Share this post


Link to post
Share on other sites
Begining of the file is where i put it... then again any file ive ever created using sessions starts like
<?
session_start();

Share this post


Link to post
Share on other sites
I know that but which one should I place the session_start in. Should it be the functions.php or the process.php or both. Does the session carry over into the function?

Share this post


Link to post
Share on other sites
are you including function.php?

'Cause if you are just make sure session_start is above the include command in process.php

Share this post


Link to post
Share on other sites
I guess I should not be lazy and test these things prior to posting..lol

Thanks for the help

Tom

Share this post


Link to post
Share on other sites

×

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.