mike_abc Posted February 29, 2012 Share Posted February 29, 2012 Hi everybody I am a PHP rookie for now, and I want to reload the same dynamic page - it's content in several languages - by clicking little flags which are located at some point on the page - AND ITS NOT WORKING. Clicking on the flag triggers a PHP script which basically says ]$_SESSION['lang'] = <Some_language>; require ('webpage.php'); where webpage.php is the dynamic page, in different languages, with it's content drawn out from a database (which contains the content in all languages I chose to display, blah, blah) Funny thing - when I look at the page html (View Page Source) - it seems OK (identical from one page to the next) ! But WHAT is displayed after the first attempt to change the language is not. Is something fundamentally wrong with my approach ? Mike Quote Link to comment https://forums.phpfreaks.com/topic/258001-php-rookie-reloading-same-dynamic-page/ Share on other sites More sharing options...
batwimp Posted February 29, 2012 Share Posted February 29, 2012 Please post as much as the relevant code from your script as you can. It's hard to gauge anything from the code you posted. And please put it inside code tags. Quote Link to comment https://forums.phpfreaks.com/topic/258001-php-rookie-reloading-same-dynamic-page/#findComment-1322467 Share on other sites More sharing options...
mike_abc Posted February 29, 2012 Author Share Posted February 29, 2012 <?php session_start(); $_SESSION['user_name']="guest"; $_SESSION['user_pswd']="..."; $_SESSION['language']="rom"; require ('source/chg_lang_rom.php'); ?> The code for chg_lang_rom.php is: <?php $_SESSION['language']="rom"; require ('source/startpage.php'); ?> The code for startpage is: <header>...</header> <body>...a table structure to generate a header with some pictures... ...then, the flags in a cell of the header table: <td bgcolor="#6C6C6C" height="30px" width="450px" align="right" valign="top"> <table border="0"> <?php include ('source/chg_lang.php') ?> </table></td> <td bgcolor="#6C6C6C" height="30px" width="66px"></td> </tr> .... </body> The code for chg_lang.php is: (it basically draws 2 little flags, other than the current language, on which you click to change the language) <?php if($_SESSION['language']=="rom") { print '<tr><td bgcolor="#6C6C6C" width="28px" height="16px" valign="middle" align="center">'; print '<a href="source/chg_lang_eng.php"><img src="images/EN_Flag.png" /></a></td>'; print '<td bgcolor="#6C6C6C" width="28px" height="16px" valign="middle" align="center">'; print '<a href="source/chg_lang_deu.php"><img src="images/DE_Flag.png" /></a></td></tr>'; } if($_SESSION['language']=="eng") { print '<tr><td bgcolor="#6C6C6C" width="28px" height="16px" valign="middle" align="center">'; print '<a href="source/chg_lang_rom.php"><img src="images/RO_Flag.png" /></a></td>'; print '<td bgcolor="#6C6C6C" width="28px" height="16px" valign="middle" align="center">'; print '<a href="source/chg_lang_deu.php"><img src="images/DE_Flag.png" /></a></td></tr>'; } if($_SESSION['language']=="deu") { print '<tr><td bgcolor="#6C6C6C" width="28px" height="16px" valign="middle" align="center">'; print '<a href="source/chg_lang_rom.php"><img src="images/RO_Flag.png" /></a></td>'; print '<td bgcolor="#6C6C6C" width="28px" height="16px" valign="middle" align="center">'; print '<a href="source/chg_lang_eng.php"><img src="images/EN_Flag.png" /></a></td></tr>'; } ?> chg_lang_eng.php & chg_lang_deu.php are very similar to chg_lang_rom.php, only the 'lang' is set to "eng" and "deu" respectively. The first time I load the page thru index.php, everything's ok. When I click on either flag, the "new" page is garbled up (no pictures, no flags etc.) Mike Quote Link to comment https://forums.phpfreaks.com/topic/258001-php-rookie-reloading-same-dynamic-page/#findComment-1322479 Share on other sites More sharing options...
batwimp Posted February 29, 2012 Share Posted February 29, 2012 You be using session_start() at the top of any page that writes to or reads from sessions. You should probably add it to the top of both chg_lang_rom.php and chg_lang.php. Try this and let me know what happens. Quote Link to comment https://forums.phpfreaks.com/topic/258001-php-rookie-reloading-same-dynamic-page/#findComment-1322483 Share on other sites More sharing options...
batwimp Posted February 29, 2012 Share Posted February 29, 2012 *should be using session_start(). Sorry about the typo. Quote Link to comment https://forums.phpfreaks.com/topic/258001-php-rookie-reloading-same-dynamic-page/#findComment-1322526 Share on other sites More sharing options...
mike_abc Posted March 1, 2012 Author Share Posted March 1, 2012 Just to make sure: Any time there is a $_SESSION['...'] in a PHP script in a html page, I should use session_start() at the beginning of that script ? This means reading, writing or checking the value of a $_SESSION variable ! So, if more than one script IN THE SAME html page does refer in any way to a $_SESSION variable, I must write session_start() at the beginning of the script ? I'll try that, but this seems completely weird to me - I mean, from a PHP philosophical point of view ! Thanks, Mike Quote Link to comment https://forums.phpfreaks.com/topic/258001-php-rookie-reloading-same-dynamic-page/#findComment-1322628 Share on other sites More sharing options...
l0gic Posted March 1, 2012 Share Posted March 1, 2012 So, if more than one script IN THE SAME html page does refer in any way to a $_SESSION variable, I must write session_start() at the beginning of the script ? I'll try that, but this seems completely weird to me - I mean, from a PHP philosophical point of view ! Correct, it used to seem weird to me too back in the day. I think it's because they call it session_start() rather than session_open() or session_continue() and your mind assumes the 'start' part is going to start a whole new session every time you use it. But no, it just lets the server know that you want to be able to access the session variables through that page. Quote Link to comment https://forums.phpfreaks.com/topic/258001-php-rookie-reloading-same-dynamic-page/#findComment-1322649 Share on other sites More sharing options...
creata.physics Posted March 1, 2012 Share Posted March 1, 2012 l0gic, you are incorrect. I've made a page called html.php, it is the ONE html page that includes two scripts, one.php and two.php. one.php does contain session_start() atop of the page. two.php does not. I can call session data in two.php as long as it is included after one.php is which initiates the session. Also, mike_abc, if you have a page that includes two scripts, not each page has to have session_start, if you have one page including two scripts, that actual page can just be the only script that calls session_start. in my case, it would be html.php that contains session_start, and then one.php and two.php would not need to have session_start atop of the page. Quote Link to comment https://forums.phpfreaks.com/topic/258001-php-rookie-reloading-same-dynamic-page/#findComment-1322655 Share on other sites More sharing options...
mike_abc Posted March 1, 2012 Author Share Posted March 1, 2012 Creata, you are right. I thought I had posted an answer after trying to include session_start() in every script (I edited the post, but it seems I didn't finally submit it, or something, because its not here). I got big, fat errors, saying session_start() had already been (executed ? I don't remember the verb exactly). I think the conclusion is this: if your scripts ARE ON THE SAME HTML PAGE, then you're not allowed to to run session_start() more than once. If you go to another HTML page, then (maybe) you can do it. But why would you, anyway ? It seems unreasonable to access a $_SESSION variable ON ANOTHER PAGE without calling session_start() - that would mean that PHP has no truely GLOBAL VARIABLES - which, for a Visual Studio programmer like me sounds absurd. Now, about my problem: The mistake was in the way I referenced the subfolders where the different elements (pictures, scripts) are located. In HTML, you need to write the path starting from the DocumentRoot of the website. So if you are referring to a picture, or HTML file, you need to write /images/picture1.gif, or /source/somepage.html. "/" will mean the DocumentRoot defined in the Apache .ini file. In PHP, I noticed the contrary: If your Apache DocumentRoot is in the include_files list, then you can (actually must !) refer to it without the preceeding "/", which in Ubuntu (which is where I develop my site) would actually meen the Ubuntu root (so an absolute reference). That means that you can have scripts all over the place, not just in your Apache DocumentRoot. At least, this is my conclusion. Please correct me if I am wrong. Thank you all, Mike Quote Link to comment https://forums.phpfreaks.com/topic/258001-php-rookie-reloading-same-dynamic-page/#findComment-1322681 Share on other sites More sharing options...
creata.physics Posted March 1, 2012 Share Posted March 1, 2012 Thanks a lot Mike. I don't think you should ever use a backslash at the end of a url, but I don't have any reasoning for this as it is just an opinion and personal preference. So I won't delve into the matter. I do recommend using the full path when rending an image or url in all html code. e.g: <img src='http://example.com/images/test.img' /> .htaccess likes to mess things up from time to time, especially if you are customizing your urls to be more search engine friendly. This can avoid quite a bit of problems for a script that runs on multiple servers, as each like to do their own thing. Also, since your main topic issue has been resolved, can you please mark this topic as solved, thank you. Quote Link to comment https://forums.phpfreaks.com/topic/258001-php-rookie-reloading-same-dynamic-page/#findComment-1322684 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.