Welcome Guest, Not a member yet? Register   Sign In
if not logged in - return to index
#1

[eluser]miauksius[/eluser]
Hi,

I have a problem. I'm creating a guestbook, and I dont want that not logged in users could read the comments in it, so i tried this:
Code:
<?php

class Guestbook extends Controller {
    parent :: Controller ( );
    if ( $this -> session -> userdata ( 'logged_in' ) != TRUE )
    {
        redirect ( 'index.php/home' );
    }
}
?>

But it doesent work. It doesent redirect at all. Why? Could you give me some tips? Cause im gonna need this in other pages too.

Thank you for your help
#2

[eluser]Dam1an[/eluser]
I think you meant to put that code inside the construtor?
Try
Code:
<?php

class Guestbook extends Controller {
    function Guestbook() {
        parent :: Controller ( );
        if ( $this -> session -> userdata ( 'logged_in' ) != TRUE )
        {
            redirect ( 'index.php/home' );
        }
    }
}
#3

[eluser]miauksius[/eluser]
It works! THANK YOU! Smile
But why does it work when I create function named the same as class (Guestbook), but not when I create function _constructor? Aren't thay the same?
#4

[eluser]Dam1an[/eluser]
The PHP5 equivalent is __construct()
It's 2 underscores and construct, not constructor

Also I just noticed your redirect function, it hould just have the controller/function names. There's no need to include index.php, as if you then get rid of index.php from the URL, you'll have to change it all manually
#5

[eluser]miauksius[/eluser]
Thanks again for those notes. But how can I get rid of that index.php from my url?
#6

[eluser]Dam1an[/eluser]
There's this wonderful thing called a user guide
Read it and it will tell you the answers
#7

[eluser]wowdezign[/eluser]
[quote author="miauksius" date="1248823682"]Thanks again for those notes. But how can I get rid of that index.php from my url?[/quote]

[quote author="Dam1an" date="1248828734"]There's this wonderful thing called a user guide
Read it and it will tell you the answers[/quote]

Speaking of which, you'll find the answer to the index.php question at:

http://ellislab.com/codeigniter/user-gui.../urls.html
#8

[eluser]Dam1an[/eluser]
@wowdezign, thats the actualy page I linked to Wink
Although hopefully he'll read the whole user guide
#9

[eluser]wowdezign[/eluser]
@Dam1an,

Sorry, I didn't even notice it was a live link. :red:
#10

[eluser]jedd[/eluser]
Computers are confusing.




Theme © iAndrew 2016 - Forum software by © MyBB