Welcome Guest, Not a member yet? Register   Sign In
functions that aren't intended to load pages
#1

[eluser]deco10[/eluser]
I would like to have a function somewhere. Presumably in my controller as thats where its used. However if somebody should happen to browse to .com/site/myFunction I don't want them to get a bunch of PHP errors. How can I make it so that the function can't be called through the URI?


I hope my lingo is understandable Smile


Thanks!
#2

[eluser]jedd[/eluser]
Howdi,

You need one of two things. The private keyword, or to prefix your function name with an underscore. The latter is documented in the CI User Guide as the way of keeping functions private, so it's probably the preferred approach. I use both, because I'm that kind of guy.
#3

[eluser]deco10[/eluser]
Ok, that makes sense and I think I have that working, but I don't actually know for sure. Heres a few bits of my code

Obviously I'm missing something or just plain doing something stupid.

Thanks!


function login()
{
$val = _authenticate("test", "testpw"); //it breaks right here
.....

}


function _authenticate($user, $pass)
{
$result = 1;

return $result;

}
#4

[eluser]pistolPete[/eluser]
You need to use this syntax:
Code:
$val = $this->_authenticate('test', 'testpw');
#5

[eluser]deco10[/eluser]
awesome, I figured it was something like that.
#6

[eluser]wiredesignz[/eluser]
[quote author="jedd" date="1244844907"]Howdi,

You need one of two things. The private keyword, or to prefix your function name with an underscore. The latter is documented in the CI User Guide as the way of keeping functions private, so it's probably the preferred approach. I use both, because I'm that kind of guy.[/quote]

Be aware that using the private function concept will generate an ugly PHP Warning because CI (being PHP4) does not realize the method is private and will still call it.

Using the underscore prefix for non-accessible methods is preferred and CI will output it's inbuilt 404 Page Not Found error.




Theme © iAndrew 2016 - Forum software by © MyBB