Jump to content

Trouble with forms & sessions.


N1CK3RS0N

Recommended Posts

Can't figure this one out!

 

I'm working on an installer for a CMS. Step 1 they select their language in a drop down menu, it also auto detects the browsers default language and makes a selection if ones found, otherwise it selects default. They can change it in the drop down menu though. When I try to change the language from English to Fresh, and then submit, the next page is still displayed in English, and the session says English.

 

I think this means something must be wrong with the form. Its telling the script when it writes the session that English was selected. Can't figure this out.

 

language.php

<?php

if (isset($_POST['submit'])){
session_start();
$_SESSION['language'] = $_POST['language'];

$url = "index.php";
header("Location: $url");
} else {

$templatefile = "language";

$default_lang_name = "English";	//Set the name for your default language.

$languages = array(	//Create an array of each language's ID and Name.
'zh' => 'Mandarin', 
'hi' => 'Hindustani', 
'es' => 'Spanish',
'en' => 'English', 
'ar' => 'Arabic', 
'pt' => 'Portuguese', 
'bn' => 'Bengali', 
'ru' => 'Russian', 
'ja' => 'Japanese',
'de' => 'German', 
'pa' => 'Punjabi', 
'te' => 'Telugu', 
'mr' => 'Marathi', 
'vi' => 'Vietnamese', 
'ko' => 'Korean', 
'ta' => 'Tamil', 
'fr' => 'French', 
'it' => 'Italian'
); 

if ( isset($_SERVER["HTTP_ACCEPT_LANGUAGE"]) ) //Check to see if the browser has a default language.
{
$language = strtolower( $_SERVER["HTTP_ACCEPT_LANGUAGE"] );	//Lowercase all letters in the returned language to avoid error.
$language = substr( $language, 0, 2 );	//Cut out the first 2 letters of the first support language.

if ( array_key_exists($language, $languages) ) //Supported.
{
	$value = $languages["$language"];
	$lang_detected = $value;
	if ( isset($_GET['language']) )	//Manual Selection.
	{
		$set_lang = $_GET['language'];
		$lang_name = $set_lang;
		include("languages/$set_lang.php");
	} else { //Auto Detection.
		$lang_name = $value;
		include("languages/$value.php");
	}
	$detect = $_LANG["language_f1_description_01"];
} else {	//Not Supported.
	$value = $default_lang_name;
	$lang_detected = $value;
	if ( isset($_GET['language']) )	//Manual Selection.
	{
		$set_lang = $_GET['language'];
		$lang_name = $set_lang;
		include("languages/$set_lang.php");
	} else { //Auto Detection.
		$lang_name = $value;
		include("languages/$value.php");
	}
	$detect = $_LANG["language_f1_description_02"];
}
} else {	//Not Found.
$value = $default_lang_name;
$lang_detected = $value;
	if ( isset($_GET['language']) )	//Manual Selection.
	{
		$set_lang = $_GET['language'];
		$lang_name = $set_lang;
		include("languages/$set_lang.php");
	} else { //Auto Detection.
		$lang_name = $value;
		include("languages/$value.php");
	}
$detect = $_LANG["language_f1_description_03"];
}

// Create drop down menu.
asort ($languages);	//Sort languages by name.

$menu = "<select name=\"language\">\n";  
foreach ( $languages as $key => $value ) 
{
if ( $lang_name == $value )	//Determine which language in the array has been selected.
{ 
	$selected = 'selected=""';
} else {
	$selected = '';
}
$menu .= "<option value=\"$value\" $selected>$value</option>\n";	//Create an option for each language in a drop down menu.
}
$menu .= "</select>\n";

require 'smarty.php';

$smarty->assign ('detect', $detect);
$smarty->assign ('select_language_menu', $menu);

require 'display.php';

}

?>

 

language.tpl

			<form action="index.php" method="post">
			<table class="content" border="0" cellpadding="0" cellspacing="0">
				<tr>
					<td>
						<div><h2>{$LANG.language_title_01}</h2><hr/></div>

						<div><h4>{$LANG.language_f1_caption_01}</h4></div>
						<div class="input_area">{$select_language_menu}</div>
						<div class="small_font">{$detect}</div>

						<br/>

						<div><input type="hidden" value="license" name="page"/><input type="submit" value="{$LANG.submit_button}" name="submit"/></div>
					</td>
				</tr>
			</table>
		</form>

Link to comment
Share on other sites

your form action is calling for index.php .. show us index.php

 

or did you typo when you put language.php as the name of the file.

 

Its just a shell.

 

<?php

switch ($_POST['page'])
{
case 'language':
include('includes/language.php');
break;
case 'license':
include('includes/license.php');
break;
case 'setup':
include('includes/setup.php');
break;
case 'database':
include('includes/database.php');
break;
case 'configure':
include('includes/configure.php');
break;
case 'finish':
include('includes/finish.php');
break;
default:
include('includes/language.php');
break;
}

?>

Link to comment
Share on other sites

looks like it is because of this in the form :

<input type="hidden" value="license" name="page"/>

that triggers the switch() which in this case is 'license' .. that come with the script or did you do that?

 

I coded it all myself. Wasn't a script.

 

I use that hidden input to tell it what the next page will be to read from in the switch in the index.php

 

But yeah, I think that is the problem. When it submits to index.php it already has the next page and goes right to that, instead of back to the previous one where it writes the sessions then reloads with the header.

 

I changed it a bit but still no dice.

 

I changed the switch to work not from $_POST['page'] but $page. That way it doesn't skip to the next page before it can write to the session. And I put $page = $_POST['page']; in the language.php file under the isset so if the forms submitted.

 

Now it wont go to the next page though. Just goes back to the language.php page.

Link to comment
Share on other sites

I got it to work using a session

 

$_SESSION['page'] = $_POST['page'];

 

and then it uses that session for the switch.

 

Only problem is if you click back to change the language, it doesn't change. Just reads whatever you submitted the first time. Hmmm. Isn't overwritting the session with the new value.

Link to comment
Share on other sites

probably because your $_SESSION['page'] is being set via a $_POST .. clicking the back button without resubmitting the form will not pass whatever value was in the $_POST['page'] input.

 

So how would I go about fixing this?

 

They would be resubmitting though. They hit back to view the previous page (languages) then they select a new language and re-click the submit button.

Link to comment
Share on other sites

proper logic and beefed up error handling.

 

usually once things get to about this point, i need a bit of a refresher .. you mind giving me a quick rundown as to where you are in the script, what you are trying to do (as a user) with the script, and what exactly is happening.

 

sorry for my ignorance, while assisting you i am also doing work of my own .. attention span is limited.

Link to comment
Share on other sites

proper logic and beefed up error handling.

 

usually once things get to about this point, i need a bit of a refresher .. you mind giving me a quick rundown as to where you are in the script, what you are trying to do (as a user) with the script, and what exactly is happening.

 

sorry for my ignorance, while assisting you i am also doing work of my own .. attention span is limited.

 

No problem.

 

What I'm working on is a content management system. Free open source software for anyone to use to make a website easily, not sure if your familiar with the concept of a CMS. This will be the installer for it. It has 6 steps.

 

1. Language (set the language for the site)

2. License (agree to the GPL for the software)

3. Setup (check file and folder write permissions and PHP, MYSQL, and XML versions)

4. MySQL (set up your information to connect to your MySQL database)

5. Configure (set general site settings such as site name and admin account)

6. Finish (create the SQL tables and write all the data collected from the installation process into the SQL tables)

 

Each step has its own file, language.php, license.php, setup.php, etc. They are all loaded though through the index.php using a switch. The person will just go to index.php and then it will switch based on the case of the 'page'. The command to go to the next page will be a hidden input. When they submit the form on step 1 (language.php) it will have the hidden input give the value of the next page to be loaded by index.php. The processing of the information submitted is done on the same page. If the form is submitted (if isset for the value submit which is the submit button) then it processes the information by writing the data into session variables and then reloads the page using the header, which will change the case in the index.php loading the next page.

 

The problem is when someone clicks the back button in the browser, to change a value they have entered. When they re-submit the form it doesn't change the value in the session from when the first form was submitted. So say you select language as French, and hit submit. It processes the value for the language and writes it as a session variable. Now the rest of the pages will read from the language file selected from the session variable. If you wanted to change this though, if you were to hit the back button in your browser and select a new language such as English and then re-submit the form, it doesn't overwrite the session variable from the first time, and the next page loads in French still.

Link to comment
Share on other sites

I think I found the solution

 

Instead of putting an if statement to process the page if the form has been submitted, else display it, I'll just put the information to write the session directly in the index.php

 

It says this at the bottom of the page though...

 

Warning: Unknown: Your script possibly relies on a session side-effect which existed until PHP 4.2.3. Please be advised that the session extension does not consider global variables as a source of data, unless register_globals is enabled. You can disable this functionality and this warning by setting session.bug_compat_42 or session.bug_compat_warn to off, respectively. in Unknown on line 0

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.