Cookies and php Version Issue

Discussion in 'PHP' started by mary.g, Dec 21, 2012.

  1. #1
    Hi ! This script seems to work perfect while I was working on it in computer but..
    after I uploaded it on server it didn't .
    In my computer I have php v.5.4.4
    my hosting pack has php v.5.3.14

    This script aims is to give user the option to chose his language only when he visiting the webpage for first time
    and then auto display his option

    Does it has to do with php version or sth else ??

    Thank you



    <!DOCTYPE html>

    <head>
    <title><?php echo $lang['PAGE_TITLE']; ?></title>
    <meta charset="utf-8">
    <link rel="stylesheet" href="style.css">
    </head>

    <?php
    session_start();
    header('Cache-control: private'); // IE FIX

    //If language is set
    if(isset($_SESSION['lang']))
    {
    $lang = $_SESSION['lang'];
    $lang = $_COOKIE['lang'];

    switch ($lang) {
    case 'en':
    $lang_file = 'en.php';
    break;

    case 'el':
    $lang_file = 'el.php';
    }

    include 'lang/'.$lang_file;

    ?>

    <center>
    <h1><?php echo $lang['PAGE_TITLE']; ?></h1>

    <div class="menu">
    <ul>
    <li><a class="menu" href="index.php"><?php echo $lang['MENU_HOME']; ?></a></li>
    <li><a class="menu" href="#about" onclick='about() '><?php echo $lang['MENU_ABOUT']; ?></a></li>
    <li><a class="menu" href="#download" onclick='download() '><?php echo $lang['MENU_DOWNLOAD']; ?></a></li>
    </ul></div>

    <script>
    function about(){
    document.getElementById('menu').innerHTML = '<?php echo $lang['CONTENT_ABOUT']; ?>';}
    function download(){
    document.getElementById('menu').innerHTML = '<?php echo $lang['CONTENT_DOWNLOAD']; ?>';}
    </script>

    <b id='menu'>
    <?php echo $lang['DESC_ABOUT']; ?>
    </b> </div>
    </center>

    <?
    }


    //If language is not set
    else {

    if(isset( $_GET['lang'])){

    $lang = $_GET['lang'];
    // register the session
    $_SESSION['lang'] = $lang;
    //setcookie() => sets cookie (name , value, expire)
    setcookie('lang', $lang, time() + (3600 * 24 * 30));
    //Redirect page after select language
    header('Location: index.php');
    }
    ?>

    <center>
    <p>Please select your Language</p><br>
    <a href="?lang=en"><img src="img/en.png" /></a>
    <a href="?lang=el"><img src="img/el.png" /></a>

    <?
    }
    ?>
     
    mary.g, Dec 21, 2012 IP
  2. mary.g

    mary.g Peon

    Messages:
    2
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Ok. I fixed it ( after many hours)

    I just had to break php pages into sth like

    <!DOCTYPE html>


    <head>
    <title><?php echo $lang['PAGE_TITLE']; ?></title>
    <meta charset="utf-8">
    <link rel="stylesheet" href="style.css">
    </head>


    <?php


    include 'translator.php';


    //If language is set
    if(isset($_SESSION['lang']))
    {
    .....
    }




    //If language is not set
    else {


    ?>


    <center>
    <p>Please select your Language</p><br>
    <a href="?lang=en"><img src="img/en.png" /></a>
    <a href="?lang=el"><img src="img/el.png" /></a>


    <?
    }
    ?>
     
    mary.g, Dec 21, 2012 IP
  3. EricBruggema

    EricBruggema Well-Known Member

    Messages:
    1,740
    Likes Received:
    28
    Best Answers:
    13
    Trophy Points:
    175
    #3
    normaly you set cookies before content is beging send to the browser...
     
    EricBruggema, Dec 22, 2012 IP
  4. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,999
    Best Answers:
    253
    Trophy Points:
    515
    #4
    Yeah, that's my take on it too... The only reason it might work locally instead of on the remote is the lack of HTTP headers on local files -- but that depends on if you're running that PHP direct or via a real server -- if the latter, then that's not the 'cause' for it working local... as it shouldn't work at all. Same goes for starting the session, should be done before ANYTHING is output.

    Typically this is why I build all my info BEFORE I even THINK about output, buffer my output, wrap all output in echo, and do not open/close php more than once per file.

    One should also have the charset meta as close to the top as possible (like say...right after <head>), actually OPEN the HTML tag, and of course I would swing an axe at all the HTML 5 asshattery.

    Stuff like this:
    if (isset($_SESSION['lang'])) {
    $lang = $_SESSION['lang'];
    $lang = $_COOKIE['lang'];

    Doesn't even make sense, since it will only ever use $_COOKIE (are you missing a closing } ?)

    Also beware that opening shorthand <? instead of <?php and not using parenthesis is no longer considered 'proper' code... support may be dropped on future versions of PHP. (while I'm hoping to see <?php ?> removed from the language entirely)

    I'd also point out that this isn't 1997, so you have no business using the CENTER tag... EVEN in HTML 5, no matter how much that rubbish spec has it's head wedged up the '90's backside... likewise there's probably no legitimate reason for the extra div around that UL since a UL is a perfectly good block level container, and if every anchor inside it is getting the same class -- NONE of them need classes.

    Of course the accessibility train wreck of that goofy javascripted onclick nonsense.... gah...
     
    deathshadow, Dec 22, 2012 IP