Jump to content

Session store on redirect (Solved)


Illasera

Recommended Posts

Hey all, I have a small issue, i can`t seems to be able to store my session after redirecting from the login page back into the main page.Cookies works fine, only sessions dont, When i display the session on the same login page, it works perfectly but when i redirect back to the main page, the session seems to be destroyed.Please note* this is done on a local server via xampp emulator.* the main page is encoded with utf-8, The encoding is stored on the file`s header, Hance i can`t use session_start() in main page because php thinks the header information is already sent.[mainpage.php login form]...Html tags ...Header tags...Body tags.

<form action="Login.php" method="get"><div style="float:right; width:500px; height:35px; margin-right:10px; background-color:#0f0f0f;"><?phpecho"<input type='checkbox' name='remember_me' value='remember' style='float:right; margin:0px; padding:0px; height:10px; width:10px; margin-right:10px; margin-top:5px;' />";echo"<input type='text' value='username' onfocus='searchfield_focus(this)' name='m_username' maxlength='32' class='styled_text_input' style='float:right; margin:0px; padding:0px; margin-right:10px;' />";echo"<input type='password' value='password' onfocus='searchfield_focus(this)' name='m_password' maxlength='16' class='styled_text_input' style='float:right; margin:0px; padding:0px; margin-right:20px;' />";echo"<input type='image' src="Enter_Account.png' alt='Enter account' style='float:right; width:100px; height:22px; margin-right:15px;' />";?>

[login form.php]

<?php$get_name=$_GET["m_username"];$cookie_state=$_GET["remember_me"];$expire=time()+360;if($cookie_state){setcookie("user", $get_name, $expire);}else{session_start();$_SESSION['user_two']=$get_name;}header('Location: /');?>

[mainpage display session]

<?php>echo "<p class='styled_text_link'>username=". $_SESSION['user_two']."</p>";print_r($_SESSION);?>

Session doesnt exist in mainpage

Link to comment
Share on other sites

You need to call session_start() on all pages you wish to access the session.

Link to comment
Share on other sites

You need to call session_start() on all pages you wish to access the session.
Please note the comment above as following : * the main page is encoded with utf-8, The encoding is stored on the file`s header, Hance i can`t use session_start() in main page because php thinks the header information is already sent.And what if i cant call the session_start function on my main page?Any alternatives?
Link to comment
Share on other sites

No - to retrieve the session, you need to send a header, i.e. through session_start(). What do you mean "the encoding is stored in the file's header"? Note you can always use buffering to prevent output being sent before you are ready.

Link to comment
Share on other sites

1Q.)What do you mean "the encoding is stored in the file's header"?2S.)Note you can always use buffering to prevent output being sent before you are ready.
1A.)When i save the .php file with utf-8 encoding, I can`t use any of the functions that required pre-processesing, such as setting cookies or sessions - The reason is that the encoding type add 3 characters with the encoding value to the file header, making the php parser think that is the real header.2Q.)How can i use a buffer to prevent output being send before am ready? Can you give a bit info/pointers/links of how to do it?
Link to comment
Share on other sites

You mean, the BOM? Have you tried saving the file without that?

Link to comment
Share on other sites

You mean, the BOM? Have you tried saving the file without that?
BOM? yep, Am using utf-8 and writing my code in notepad, so yea, it will add the BOM by default, am not sure i can avoid it.Am encoding the file because am not using ASCII language, Not my native / website language.I should look in Notepad++ Maybe ill have the option there.Solved, Saved without BOM, Thanks again synook, Third time in a row that you helped me
Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...