Help - Search - Members - Calendar
Full Version: Help With A Check_login
Zymic Webmaster Forums > Web Design & Development > Server Side Scripting > PHP
MrTouz
Ok, i have this php code :

CODE
<?php
session_start();
include ("functions.php");
check_login();
?>


this checks if my client is logged in or not.

If yes, than it is showing some info that i put just under it.
If not, than its putting the login form.

___

What i went is IF the client is not login i want him to be redirected to login.php, right now what it does is it takes the code from login.php and put it inside this page (in case he is not logged in)... i don't want that this code brings the form but i want this code to redirect the client to the form (login.php)

This is my function.php :

CODE
<?php
if (eregi ("functions.php",$_SERVER['PHP_SELF'])) die ("<font color=\"red\">error</font>");
function check_login ()
{
    if (!isset ($_SESSION['loggedin']) && !isset ($_SESSION['time']))
    {
        require ("login.php");
        die ();
    }
    else
    {
        session_start();
    }
}
ob_start();
?>


I thought of puting some meta redirect code inside the else { } but i really suck at php, and donno what to really do.

Thanks for the help smile.gif

Oh also, if anyone knows a logout script. (donno if any logout scripts work or do i need a specific one taken from my login)

Thanks :
Ed
Use header:

Replace:
CODE
require ("login.php");
die();


With:
CODE
header('Location: login.php');
exit;


Die and exit are synonymous, I just prefer exit.
MrTouz
I just tried it out and it is not showing, or redirecting.

i took the code back to what it was and its working, well showing the login as "require" but no redirect :/

Can't i use something like a 'print' and put a meta redirect ?

this is the code once modified :

CODE
<?php
if (eregi ("functions.php",$_SERVER['PHP_SELF'])) die ("<font color=\"red\">error</font>");
function check_login ()
{
    if (!isset ($_SESSION['loggedin']) && !isset ($_SESSION['time']))
    {
        header('Location: login.php');
        exit;
    }
    else
    {
        session_start();
    }
}
ob_start();
?>


Is it because my pages are index.php?id=something ?

(i did try location: ?id=login as well but did not work... also...)
Ed
QUOTE(MrTouz @ Jan 30 2008, 03:02 PM) *
I just tried it out and it is not showing, or redirecting.

i took the code back to what it was and its working, well showing the login as "require" but no redirect :/

Can't i use something like a 'print' and put a meta redirect ?

this is the code once modified :

CODE
<?php
if (eregi ("functions.php",$_SERVER['PHP_SELF'])) die ("<font color=\"red\">error</font>");
function check_login ()
{
     if (!isset ($_SESSION['loggedin']) && !isset ($_SESSION['time']))
     {
         header('Location: login.php');
         exit;
     }
     else
     {
         session_start();
     }
}
ob_start();
?>


Is it because my pages are index.php?id=something ?

(i did try location: ?id=login as well but did not work... also...)


Are you outputting anything before it? You can do it as a meta refresh but it's not the prefered way.
MrTouz
I do not know what you mean but outputting anything ?

I tried meta but did not work, i think it is because i already opened and closed <head></head> inside my index and that this code is not inside the <head> tag.

I did this :

CODE
<?php
if (eregi ("functions.php",$_SERVER['PHP_SELF'])) die ("<font color=\"red\">error</font>");
function check_login ()
{
    if (!isset ($_SESSION['loggedin']) && !isset ($_SESSION['time']))
    {
        print("<script type=\"text/javascript\">
<!--
window.location = \"?id=login\"
//-->
</script>");
        die ();
    }
    else
    {
        session_start();
    }
}
ob_start();
?>


and it worked, but it looks ugly tongue.gif
Ed
QUOTE(MrTouz @ Jan 30 2008, 03:31 PM) *
I do not know what you mean but outputting anything ?

I tried meta but did not work, i think it is because i already opened and closed <head></head> inside my index and that this code is not inside the <head> tag.

I did this :

CODE
<?php
if (eregi ("functions.php",$_SERVER['PHP_SELF'])) die ("<font color=\"red\">error</font>");
function check_login ()
{
    if (!isset ($_SESSION['loggedin']) && !isset ($_SESSION['time']))
    {
        print("<script type=\"text/javascript\">
<!--
window.location = \"?id=login\"
//-->
</script>");
        die ();
    }
    else
    {
        session_start();
    }
}
ob_start();
?>


and it worked, but it looks ugly tongue.gif


Very ugly and not the correct way to go about a page redirection tongue.gif . Make sure there's no output as in echo's or HTML prior to calling the header('Location: login.php') else it will not work. Make sure errors are on so you can get any errors that occur.
MrTouz
Ok, reason why i am using a refresh and not a require function is because when i use my require anything after the code is gone.

When i go to my clients page and i am not logged in it does : require login.php. So the login form shows up my site but my sites footer is gone :/ anything after the form is just deleted and does not appear on my browser.

When i go directly on my login page without going from the client page the footer is there.

So that is why i am using redirect so instead of showing me the login form it sends me to it.

All that because the footer is gone.

____

I did make the meta work so :

CODE
<?php
if (eregi ("functions.php",$_SERVER['PHP_SELF'])) die ("<font color=\"red\">error</font>");
function check_login ()
{
    if (!isset ($_SESSION['loggedin']) && !isset ($_SESSION['time']))
    {
        print("<head><META HTTP-EQUIV=\"refresh\" content=\"0;URL=?id=login\"></head>");
        die ();
    }
    else
    {
        session_start();
    }
}
ob_start();
?>


Working good, but i still prefer the header location.

by this :

QUOTE
Make sure there's no output as in echo's or HTML prior to calling the header('Location: login.php') else it will not work


do you mean i need to make sure that there is no code (somewhere) coming before my header('Location: login.php') ?

Inside my function.php... nope nothing before, its just the code i showed you above. as well as my clients page its the very first code. but inside my login :

i have this part that bugs me a little :
CODE
        if ($search_login3)
        {
            //valid login!
            if (isset ($rmbpw))
            {
                //set these cookie to remember the user next time he logs in.
                setcookie ('username', $username, time()+1209600, '/', '', 0);
                setcookie ('password', base64_encode ($password), time()+1209600, '/', '', 0);
            }
            //if don't wish to continue remembering username and password
            else if(empty ($rmbpw) && isset ($_COOKIE['username']) && isset ($_COOKIE['password']))
            {
                //remove these cookie.
                setcookie ('username', '', time()-60, '/', '', 0);
                setcookie ('password', '', time()-60, '/', '', 0);
            }
            //start the sessions
            //the line is to be replaced on top line of page
            //session_start();
            //remove the anti-hacking cookie-feature-redundant now
            //setcookie ('tries', '', time()-60, '/', '', 0);
            $_SESSION['loggedin'] = $_POST['username'];
            $_SESSION['time'] = time();
            //header ('Location: '.$PHP_SELF);
            //exit;
            if ($development == TRUE) print "<meta http-equiv=\"Refresh\" content=\"0; url=".$PHP_SELF."\">";            
            else print "<meta http-equiv=\"Refresh\" content=\"0; url=".$PHP_SELF."\">";
        }


i see the header location code and some meta as well. and that is on my login.php

Can this be blocking the header(location... ?

Thanks a LOT for the help smile.gif
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2012 Invision Power Services, Inc.