tags:

views:

126

answers:

3

How can you make an own function in PHP such that I can bind to it different files according to the data in $_GET?

I use the handler handler_login.php which only shows the login form if you are not logged in.

However, I need to run specific operations if the user is logged_in or not_logged_in. My current hnadler however is not so flexible to achieve this.

My handler_login.php

<?php
// Check for the existing Cookie

if (isset($_COOKIE['login']) ) {
    //1. read the first word in Cookie of the form 
        //"[email protected],ca05106e445c15197f7213bc12648524
    //Then, store this word to $email 
    $cookie_tripped = explode(",", $_COOKIE['login']);   
    $email = $cookie_tripped[0];
    $result = pg_prepare($dbconn, "query1", 'SELECT passhash_md5 FROM users 
                         WHERE email = $1;');
    $result = pg_execute($dbconn, "query1", array($email));

    if(!$result) {
        exit;
    }

    // to take the passhash out of the cookie
    $passhash_md5_cookie = $cookie_tripped[1];
    if($result == $passhash_md5_cookie) {
        header("Location: ../index.php");
        die("logged in");
    }
    // put the login form visible                // Problem HERE: I want that my function changes this value in the parameter
    include 'forms/login.php';
}

?>

The problem in the above code is that it is not extensible.

The problem suggests me that I need two functions: not_logged_in(include_file) and logged_in(include_file). The former should include the file in the parameter if the user not logged in. Otherwise, it should return nothing nothing new to the display.

The latter should then again include the file in the parameter if the user is logged in. Otherwise, it should do nothing.

My attempt in Pseudo code.

*Not_logged_in(paramer) -function*

 // process all variables in the URL such as ?questions=777
 not_logged_in(forms/login.php)
 // this should return
 //     include 'forms/login.php'; if the user is not logged in

*Logged_in(paramer) -function*

 // process all variables in the URL such as ?questions=777
 logged_in(handler/send_question.php)
 // this should return
 //     include 'handlers/send_question.php'; if the user is logged in
+3  A: 
function not_logged_in($file)
{
    if(!isset($_COOKIE['login']))
    {
       include($file);
    }
}
function logged_in($file)
{
    if(isset($_COOKIE['login']))
    {
       include($file);
    }
}
not_logged_in("login.php");
logged_in("handler/send_question.php");

This is assuming that if $_COOKIE['login'] is set, they are logged in.

I actually think that is an interesting way to do it instead of just doing an if statement:

if(isset($_COOKIE['login']))
{
    include("handler/send_question.php");
}
else
{
    include("login.php");
}

The problem with your way however is that it is extra code because it has to evaluate two functions. But if you never actually evaluate code (because only one function will pass the $_COOKIE test) in one of the functions it could work.

You cannot pass GET parameters to included files like this:

include("test.php?somevar=var");

Included files inherit the $_GET parameters from the page being called.

Chacha102
You probably mean $_SESSION instead of $_COOKIE.
MrHus
According to his example code, it is $_COOKIE.
Chacha102
Why should I use $_SESSION to see the login status?
Masi
$_SESSION keeps the variables on the Server Side and allows you to interact with it more like an array.
Chacha102
@Cha: How should I change the code to use $_SESSION, instead of $_COOKIE?
Masi
A: 

I think you're taking the wrong approach. You say that both functions should process all variables if the user is logged in, and both functions should return a template if the user is not logged in. So it sounds like you'd be repeating yourself if you wrote two functions to do this.

I would use one function, let's call it loginProcess(), and pass it nothing. It can then call a second function isLoggedIn(), and make it's decisions based on that:

function loginProcess()
{
    $loggedIn = isLoggedIn();   //returns boolean true or false
    if ($isLoggedIn) {
         //process variables here
         //show logged in template here
    }
    else {
         //show not logged in template here
    }
}

If you need separate functions for processing, then split it up further into that:

function loginProcess()
{
    if (isLoggedIn()) processLoggedInUser();
    else processNotLoggedInUser();
}

Something like this will save you from having to maintain multiple functions that process the same variables.

zombat
+1  A: 

You can create a function in the following matter:

function func_name($params) {
    // operations
}

So, for your example, you could write the following:

function handle_login($if_logged, $if_not_logged) {
    if(isset($_COOKIE['login'])) {
        include($if_logged);
    } else {
        include($if_not_logged);
    }
}

Keep in mind that because you are including inside a function, variables outside the function will not be visible to the included file. You'll have to use global or the $GLOBALS super-global to access them. For example, you won't be able to read the variable $cantSeeMe in the example below:

$cantSeeMe = 'Ha! Ha! I am hidden!';

function test() {
    var_dump($cantSeeMe);
}

test() // dumps null

More information on variable scoping available here:

PHP: Variable Scope

Andrew Moore