Jump to content

Using GET to change image based on URL


galvin

Recommended Posts

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!

Link to comment
Share on other sites

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? 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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'];
}

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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";
}

Link to comment
Share on other sites

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";
}

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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!

Link to comment
Share on other sites

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.

 

Link to comment
Share on other sites

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.

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.