-1

I am having a bit of an issue, probably over thinking it but i would appreciate any help. I have a main domain. so, domain.com.. which as a landing page is set to have city names in which you can click on to go to a subdomain.... location1.domain.com, I am trying to store the link as a variable to call $_COOKIE.

INDEX.PHP

if($_COOKIE['location'] == "")
{
    ?>
    <body>
    <center> Please select your city. </center>
    <center> Location 1 </center>
    <br><br><br>
    <table width="100%">
       <tr>
        <td width="50%" align="center">
<a href='http://location1.domain.com' onclick="setCookie('location', 'location1',)">location1</a>
         <td width="50%" align="center">
<a href="http://location2.domain.com" onlick="SetCookie('location','location2')">location2</a>
            </td>
        </tr>
    </table>
    <?
} 
else 
{
?>
    <html>
    <head>
    <meta http-equiv="refresh" content="0; url=http://"<?$_COOKIE['location']?>".domain.com">';
    echo '</head>';
    echo '</html>';
<?  
}
?>

I want it to be so that when I click on the link that goes from the main index that it saves my selection in a cookie so that next time I visit domain.com it automatically redirects me to my previous choice.. I have also tried using a js to facilitate this but I must not be doing it correctly... everything seems to work except the part where it saves the cookie... thanks in advance!

  • 3
    `$_Cookie` is not the same as `$_COOKIE` – Yang Jun 05 '13 at 03:56
  • 2
    And why in 2013 you still use `href="http://location2.domain.com" onlick="SetCookie`? This considered as a bad practice. HTML markup and its event handlers should be totally decoupled. – Yang Jun 05 '13 at 03:58
  • 4
    @metal_fan [instead of passing judgement](http://xkcd.com/1053/), how about explaining why it's a bad idea and how to avoid it/solve it a different way? – nhinkle Jun 05 '13 at 17:26
  • 2
    http://stackoverflow.com/questions/12627443/jquery-click-vs-onclick – Karoly Horvath Jun 05 '13 at 17:36
  • @nhinkle I believe I did. Have you ever heard about Separation of Concerns (SoC)? We'd never mix javascript event handlers with HTML "markup". Why? AGAIN : it breaks the SoC and the SRP as well. A different way? Well, the thing like `$("#selector").click()` must be obvious... – Yang Jun 05 '13 at 18:12
  • @metal_fan obviously, it's not obvious, since the user wasn't doing it, right? I have not heard SoC referred to as such, and I doubt the OP has either. That's my point: if somebody isn't following best practices, don't just say "you're doing it wrong!!", _explain_ to them how to do it right (or provide them the resources to learn themselves, like Karoly did). – nhinkle Jun 05 '13 at 18:21
  • If anything was obvious I wouldn't be posting here in the first place. I do believe I was overly complicating things.. so instead of the onclick, I just have a link that goes over to location1 and another for location2, and I am trying to set the cookie at the top of the code in their respective index.php.. however the domain.com/index.php is not picking up the cookies from location1.domain.com or from location2.domain.com, even though it shows up in my browser cookies when I do a search for them – Michael Clements Jun 05 '13 at 18:57
  • possible duplicate of [Reference: Why does the PHP code in my Javascript not work?](http://stackoverflow.com/questions/13840429/reference-why-does-the-php-code-in-my-javascript-not-work) – deceze Jun 06 '13 at 08:26

1 Answers1

0

first of all you should not use this style of coding at all. Its totally open for all kinds of code injection.

Some advises to your code:

Use <?=$_COOKIE['location']?> to print out your stored cookie value. Their is no native setCookie function in javascript. You have to use

document.cookie = 'cookiename=cookievalue; expires=Thu, 1 Aug 2013 20:30:20 UTC; path=/';

for example. Try some changes and don't hesitate to ask again.

Best Regards

Stefan
  • 21
  • 3