Page 1 of 1

[SOLVED] Session trouble

Posted: Wed Jul 07, 2004 3:37 am
by oyse
Bech100 | Please use

Code: Select all

tags when posting code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]

I am having a trouble with sessions. The code works fine in Opera, but in Mozilla I get into trouble ( this might be a mozilla bug, but what do I know ).

I have a login in page as follows.

Code: Select all

<?php

session_start();
$knownUser;
if( !empty($_POST[ 'username' ]) && !empty( $_POST[ 'password' ] ) ){
//athenticate
}

if( $knownUser ){
//redirect user to index page
if( isset( $_SESSION[ 'pageSession' ] ) ){
echo 'isset';
} 
}else{
//create login page
}
?>
If the user is authenticated he is redirected to a index page with the following code:

Code: Select all

<?php
session_start();

$page;

//a session has not been created
if( !isset( $_SESSION[ 'pageSession' ] ) ){
//redirect back to login
 }else{
//show page
}

?>
For some reason this works fine Opera, but not in Mozilla In Mozilla the session is active when the user is authenticated because it prints isset, but when the user comes to the index page he is redirect back to the login page. Has anyone encountered this problem before? I am doing something wrong? If this is a commen problem is there a work around?

Bech100 | Please use

Code: Select all

tags when posting code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]

Posted: Wed Jul 07, 2004 4:33 am
by Skittlewidth
can you post the redirect part of the script?

Posted: Wed Jul 07, 2004 4:47 am
by oyse
This is the function that creates the redirect header. $this->redirectAdress is either index.php or login.php

Code: Select all

function createRedirectHeader(){
    $newline = "\n";

    echo '<?xml version="1.0" encoding="UTF-8"?>' . $newline ;
    echo '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"';
    echo '"http://w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">' . $newline;
    echo '<meta http-equiv="refresh" content="3;URL=' . $this->redirectAdress . '">' . $newline;
    echo '<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">' . $newline;
    echo '<head>' . $newline;
    echo '<title>' . $this->title . '</title>' . $newline;
    echo '<link rel="stylesheet" href="' . $this->stylesheet . '" />' . $newline ;
    echo '</head>' . $newline;
//this didn't help    
//echo '<body onLoad="window.location=''' . $this->redirectAdress . '''" >' . $newline;
}

Posted: Wed Jul 07, 2004 5:46 am
by launchcode
That isn't valid 1.0 Transitional XHTML, so yes it is likely it wouldn't work in Mozilla.

Posted: Wed Jul 07, 2004 6:43 am
by oyse
Thank you. I didn't know the <meta> tags had be inside the <head> tag and there where some other bugs there to. Now everything is working just fine in Mozilla as well.

Here is my new and improved code which produces valid 1.0 transitional XHTML if anyone is interrested

Code: Select all

function createRedirectHeader(){

    $newline="\n"; //used to format html output.

    echo '<?xml version="1.0" encoding="UTF-8"?>' . $newline ;
    echo '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" ';
    echo '"http://w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">' . $newline;
    echo '<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">' . $newline;
    echo '<head>' . $newline;
    echo '<meta http-equiv="refresh" content="3;URL=' . $this->redirectAdress . '" />' . $newline;
    echo '<title>Redirect</title>' . $newline;
    echo '<link rel="stylesheet" href="stylesheets/main.css" />' . $newline ;
    echo '</head>' . $newline;
    echo '<body>' . $newline;
    echo '<h3>If you are not directly redirected press <a href="' . $this->redirectAdress . '"> this </a>link</h3>';
    echo '</body></html>';

  }