Jump to content

PHP Rookie: Reloading same dynamic page


mike_abc

Recommended Posts

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

 

Link to comment
Share on other sites

<?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

 

Link to comment
Share on other sites

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

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

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.