galvin Posted September 5, 2011 Share Posted September 5, 2011 Main URL of website home page: www.mysite.com and this shows image "A" in the header. I want to have a marketing campaign which will send people to my site via this link: www.mysite.com/index.php?ref=att When people get to my site via the 2nd link, I want to show image "B" instead of image "A". I put this at top of home page... require_once("includes/session.php"); if (isset($_GET['ref']) && ($_GET['ref'] == "att")) { $_SESSION['ref'] = "att"; } else { } and this code is in my header include file, a little further down on the page... <?php if (isset($_SESSION['ref']) && $_SESSION['ref'] == "att") { echo "<span class='phone'><img src='/images/B.png' /></span>"; } else { echo "<span class='phone'><img src='/images/A.png' /></span>"; } ?> Does this look right? because I can't get it to work. It's showing image A regardless of which URL I use. I have to be missing something obvious because I really thought this would be super easy to do. If anyone sees any problems, let me know. But I'll keep testing and hopefully find my mistake. Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/246439-using-get-to-change-image-based-on-url/ Share on other sites More sharing options...
Pikachu2000 Posted September 5, 2011 Share Posted September 5, 2011 Have you echoed (or preferably var_dump()ed) $_SESSION['ref'] to make sure its value is what you expect it to be? Quote Link to comment https://forums.phpfreaks.com/topic/246439-using-get-to-change-image-based-on-url/#findComment-1265485 Share on other sites More sharing options...
galvin Posted September 5, 2011 Author Share Posted September 5, 2011 Yep, $_SESSION['ref'] is correctly echoing simply "att", so something must be wrong with my 2nd bit of code... Quote Link to comment https://forums.phpfreaks.com/topic/246439-using-get-to-change-image-based-on-url/#findComment-1265495 Share on other sites More sharing options...
MasterACE14 Posted September 5, 2011 Share Posted September 5, 2011 there doesn't appear to be anything wrong with your code. Something must be changing/unsetting $_SESSION['ref'] somewhere else in your script. Quote Link to comment https://forums.phpfreaks.com/topic/246439-using-get-to-change-image-based-on-url/#findComment-1265496 Share on other sites More sharing options...
galvin Posted September 6, 2011 Author Share Posted September 6, 2011 I found that if I include the code for my "dynamic" header in the index.php page directly, it works. But if I have the code in a separate header.php file which I include via the code below, it does NOT work... require_once("http://mysite.com/includes/header.php"); How does that make sense? Do SESSION variables work differently when used with inlcude files? Quote Link to comment https://forums.phpfreaks.com/topic/246439-using-get-to-change-image-based-on-url/#findComment-1265799 Share on other sites More sharing options...
cyberRobot Posted September 6, 2011 Share Posted September 6, 2011 I found that if I include the code for my "dynamic" header in the index.php page directly, it works. But if I have the code in a separate header.php file which I include via the code below, it does NOT work... require_once("http://mysite.com/includes/header.php"); How does that make sense? Do SESSION variables work differently when used with inlcude files? When using a .php extention for the include, variable scope seems to change. Maybe sessions are affected also. Have you tried adding session_start() to header.php? You could also look into changing the include to something like header.html Quote Link to comment https://forums.phpfreaks.com/topic/246439-using-get-to-change-image-based-on-url/#findComment-1265934 Share on other sites More sharing options...
AyKay47 Posted September 6, 2011 Share Posted September 6, 2011 do you have session_start() in your code anywhere? Quote Link to comment https://forums.phpfreaks.com/topic/246439-using-get-to-change-image-based-on-url/#findComment-1265943 Share on other sites More sharing options...
galvin Posted September 6, 2011 Author Share Posted September 6, 2011 Yep, I have session_start() at the top of my index.php. I just added it to the top of the header include file as well, but that didn't change anything. I'll keep messing with it. Seems so strange. Quote Link to comment https://forums.phpfreaks.com/topic/246439-using-get-to-change-image-based-on-url/#findComment-1265946 Share on other sites More sharing options...
AyKay47 Posted September 6, 2011 Share Posted September 6, 2011 if you keep using the else conditional... perhaps you should output the session to see what you are getting.. if (isset($_SESSION['ref']) && $_SESSION['ref'] == "att") { echo "<span class='phone'><img src='/images/B.png' /></span>"; } else { print $_SESSION['ref']; } Quote Link to comment https://forums.phpfreaks.com/topic/246439-using-get-to-change-image-based-on-url/#findComment-1265950 Share on other sites More sharing options...
cyberRobot Posted September 6, 2011 Share Posted September 6, 2011 Did you try echoing $_SESSION['ref'] throughout the program to see where it's getting lost? For example, I would attempt to echo it before, inside, and after the "require_once("http://mysite.com/includes/header.php");" code. Note that you'll want to echo something else with the variable. That way if the variable is empty, it will be easier to tell. For example: echo "({$_SESSION['ref']})"; Or you could use var_dump() http://php.net/manual/en/function.var-dump.php Quote Link to comment https://forums.phpfreaks.com/topic/246439-using-get-to-change-image-based-on-url/#findComment-1265951 Share on other sites More sharing options...
galvin Posted September 6, 2011 Author Share Posted September 6, 2011 I did what CyberRobot suggested and the session variable is blank before, it is set when inside, and it is blank after. As for when it gets set inside, it's setting it, but setting it incorrectly. So I gotta resolve this part before I worry about the rest. The url is http://www.mysite.com/index.php?ref=att and my code is below. It keeps setting it to "no url appendage", but it clearly should be set as "att", right? This should be simple and it keeps getting weirder if (isset($_GET['ref']) && ($_GET['ref'] == "att")) { $_SESSION['ref'] = "att"; } else { $_SESSION['ref'] = "no url appendage"; } Quote Link to comment https://forums.phpfreaks.com/topic/246439-using-get-to-change-image-based-on-url/#findComment-1265974 Share on other sites More sharing options...
AyKay47 Posted September 6, 2011 Share Posted September 6, 2011 the only thing that catches my eye is the parenthesis around your second condition are not needed.. if (isset($_GET['ref']) && $_GET['ref'] == "att") //remove parenthsis { $_SESSION['ref'] = "att"; } else { $_SESSION['ref'] = "no url appendage"; } Quote Link to comment https://forums.phpfreaks.com/topic/246439-using-get-to-change-image-based-on-url/#findComment-1265975 Share on other sites More sharing options...
cyberRobot Posted September 6, 2011 Share Posted September 6, 2011 It's a little difficult to tell what's going wrong without seeing more code. So is there a reason why you're setting the session variable in two different spots? Quote Link to comment https://forums.phpfreaks.com/topic/246439-using-get-to-change-image-based-on-url/#findComment-1265986 Share on other sites More sharing options...
galvin Posted September 6, 2011 Author Share Posted September 6, 2011 Sorry, here is where I'm at now. I moved this code below to the top of the home page (index.php) and it works properly (i.e. when the url of mysite.com/index.php?ref=att is loaded, the $_SESSION['ref'] variable is set properly to "att". FYI, I am only setting it to "no url appendage" for testing purposes, but generally the "else" would be blank. if (isset($_GET['ref']) && $_GET['ref'] == "att") { $_SESSION['ref'] = "att"; } else { $_SESSION['ref'] = "no url appendage"; } Then, further down in my index.php page, I call an include file this way... require_once("http://mysite.com/includes/header.php"); And in that include file, there is nothing but this... <?php if (isset($_SESSION['ref']) && ($_SESSION['ref'] == "att")) { echo "<span class='phone'>MONKEYS</span>"; //I just changed it to this text for testing } else { echo "<span class='phone'><img src='http://mysite.com/images/phonenum.png' /></span>"; } ?> But it's become clear that this include file is not receiving the $_SESSION['ref'] variable because when I echo $_SESSION['ref'] anywhere on the index.php page itself, it shows "att". But when I echo it inside the include file, it's blank (i.e. ""). If I move the code from the include file onto index.php itself, it works fine. But I have a ton of pages with this header so I need to have it happen in the include file. Any thoughts? Quote Link to comment https://forums.phpfreaks.com/topic/246439-using-get-to-change-image-based-on-url/#findComment-1266000 Share on other sites More sharing options...
premiso Posted September 6, 2011 Share Posted September 6, 2011 require_once("http://mysite.com/includes/header.php"); Why are you including it from the URL? That will parse the page and deliver the end user content. Do this instead: require_once("/includes/header.php"); And see how that treats ya. Quote Link to comment https://forums.phpfreaks.com/topic/246439-using-get-to-change-image-based-on-url/#findComment-1266010 Share on other sites More sharing options...
galvin Posted September 6, 2011 Author Share Posted September 6, 2011 Well that treated me pretty damn good. Thank you premiso! It had to be changed to just "includes/header.php" (no leading slash), but it works now. Now let me sound like an old man and ask, "What's that you say about it parsing the page if you include the full URL?" I didnt know that mattered (obviously). You're telling me things are handled differently if you use the full URL vs. just he directory. I don't get why, but I'm sure glad I know that now Thanks again! Quote Link to comment https://forums.phpfreaks.com/topic/246439-using-get-to-change-image-based-on-url/#findComment-1266016 Share on other sites More sharing options...
premiso Posted September 6, 2011 Share Posted September 6, 2011 I didnt know that mattered (obviously). You're telling me things are handled differently if you use the full URL vs. just he directory. I don't get why, but I'm sure glad I know that now When you view your page via the http protocol you are seeing a page that has been generated by the server. So all you see is the "output" that the end user is suppose to see. If you view the source, all you see is the HTML generated by the server. When you include a file via the HTTP protocol, that is all you are essentially getting. So unless your server did not parse PHP, you are just including the end result HTML. When you do the include locally, the php file is not parsed, in other words the webserver is not generating the output for the end user, you get the raw code which include is able to interpret and actually include / use. Quote Link to comment https://forums.phpfreaks.com/topic/246439-using-get-to-change-image-based-on-url/#findComment-1266020 Share on other sites More sharing options...
galvin Posted September 6, 2011 Author Share Posted September 6, 2011 Great explanantion, thank you so much!!! Quote Link to comment https://forums.phpfreaks.com/topic/246439-using-get-to-change-image-based-on-url/#findComment-1266026 Share on other sites More sharing options...
cyberRobot Posted September 6, 2011 Share Posted September 6, 2011 Well that treated me pretty damn good. Thank you premiso! It had to be changed to just "includes/header.php" (no leading slash), but it works now. For what it's worth, if you want to use the leading slash, you just need to add the document root info: <?php require_once($_SERVER['DOCUMENT_ROOT'] . "/includes/header.php"); ?> Note that using a root-relative link is beneficial if you copy & paste template code from one page to another...and some of the pages exist deeper in the directory structure. Quote Link to comment https://forums.phpfreaks.com/topic/246439-using-get-to-change-image-based-on-url/#findComment-1266029 Share on other sites More sharing options...
galvin Posted September 6, 2011 Author Share Posted September 6, 2011 Cool, thanks for the info cyberRobot! Quote Link to comment https://forums.phpfreaks.com/topic/246439-using-get-to-change-image-based-on-url/#findComment-1266030 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.