View Full Version : PHP Dynamic redirect header help

01-06-2010, 05:44 PM

Excuse me if I'm a little long winded explaining this....

I have a site that will offer members a range of services including their own web site which they can point their customers to (on our site, for example: mysite.com/site/index.php?mem_id=1).

So I have a choice of web design styles for the members, they can choose the style they want from a drop down in a form in their member area and save their style choice in the mysql db.

I have a landing page (mysite.com/site/index.php?mem_id=1) which the member can direct their domain or friends to so they can see that members web page.

I need the best solution (without putting a strain on the linux server) to redirect based on the mem_id and style_id so that the member web site appears and the site style the member has set.

I had hoped this would work:

header('Location: '.echo $resArray['mem_des']. '/index.php?mem_id='.$mem_id.'');

but of course I have code before the header that pulls the member info and I know the header should come first. Perhaps I should put the pulled info into a variable and call it in the header somehow?

Any code ideas you have would be great.

Pie (worn out intermediate php coder). :thumbsup:

01-06-2010, 06:31 PM
EDIT ...

I read your post wrong, so I'm changing my answer.

Is the "profile" site information for your members saved in a table, like a members profile?

When any person adds ?mem_id to the URL, it will save that in a SESSION.
Then as the person navigates the site, they will be accessing the headers, banners, colors, fonts,
from the member's profile ... because it can query your database with the mem_id session variable.

So, after the session variable is set, you no longer need to have that variable added to the URL.
Only the first time it's accessed does it need to know the mem_id.

You could just as easily use a person's name too ... or a "username".


01-07-2010, 12:22 AM

Re: Is the "profile" site information for your members saved in a table, like a members profile?...

...Yep, and it pulls the member info from this based on mem_id that's passed. But I am a little confused, what would the header redirect code look like? I'd like to try anything you can show me.

Thanks mlseim.

01-07-2010, 12:27 AM
header('Location: '.$resArray['mem_des']. '/index.php?mem_id='.$mem_id);
That should work but that also depends on what the field mem_des holds.

01-07-2010, 12:41 AM
The very top of your "index.php" file, you will look for the URL variable,
and also check for an existing session ...

You would give them a link like this: mysite.com/site/index.php?mem_id=234

session_start(); // This is the first line, you'll be using PHP SESSIONS

// Look for the URL variable

// Check for an existing session ... if it's already started, then use the session instead.
// The session has not been started, so set it and restart the page.
$_SESSION['MemberID'] = $mem;
header ("location: index.php");

// Now you have a session set, you can see the member's ID any time you want.

// Say you have a banner or header that is named with the member's ID number,
// Example: header_234.php

// Start the HTML output ...
<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">
<html xmlns=\"http://www.w3.org/1999/xhtml\">

// include the correct header
// or query your database here and pick out the correct header based on $mem (the user's id) ...
$header = "header_".$mem.".php";

// do the same for body

// do the same for any other content

// end your page ...


01-07-2010, 08:44 AM
Thanks guys with your help its working.


01-07-2010, 03:11 PM
Actually this has thrown up another issue and that is if the member changes the web design styles, I need the page that may have been bookmarked by a visitor to check the design style in use and redirect so the visitor sees the web design style the member has set.

For example if the aboutus.php was bookmarked by a visitor, but the member changed the style choice (which means files in a different folder) I thought it might be a straight forward if else statement with header redirect:
<?php header('Location: '.$resArray['mem_des']. '/aboutus.php?mem_id='.$mem_id); ?>

But I can't get my head around the way the code should be written. :eek:

I guess the code needs to check current web design style setting (not just on the index page but also for the aboutus and contactus pages) then redirect so even if one web design style is bookmarked, the code checks and redirects.

Here are certain styles they may be directed to:


Any code you could show me would help. Thanks.

01-07-2010, 03:24 PM
hmmm ... you're right.
If the visitor bookmarks one of the other member's pages,
it won't know the mem_id when they return because the session will be gone.

You could use cookies instead of sessions, but people delete their cookies sometimes.

The only way I can think of, is that each member has their own directory,
so any bookmarks will always have that unique URL with the directory indicated.
Example www.memsite.com/234/aboutus.php

In fact, if each member were given their own directory, that would solve almost
all of the other problems as well. A visitor would stay within the members' directory
the whole time, yet the scripts in all those members' directories would be using the
same database and other stylesheets and templates as needed.

The member's own directory referenced by either their mem_id or actual username?

01-07-2010, 05:52 PM
Thanks mlseim, but I think giving a directory to however many members (and there could be many) would not be efficient. I was thinking more like this...

If the visitor had bookmarked this page:


and visits that page, the code on this page would need to check the mysql db for mem_id=1 and the web design style that has been set. Then either display (using else if statement presumably) memsite/styleone_green/aboutus.php?mem_id=1 (if 'styleone_green' web design style is what has been set by the member), or redirect to for example memsite/styletwo_red/aboutus.php?mem_id=1 (if 'styletwo_red' web design style is what has been set by the member).

01-07-2010, 07:13 PM
Then you'll have to attach that mem_id variable to EVERY URL you have.

And you're correct ... every page (script) would need to check the database
to figure out which style to use.

In most situations, a user logs-in and uses the site. You can easily offer the
correct styles because you know who is logged-in. In your case, ANY visitor
will want to display a particular "members pages" without logging in. So, you have
to constantly know which member's page style to display because you have no
idea who the visitor is. Either by using separate directories, attaching variables
to the URL each time, or making the visitor pick the member's page before they
can view them ... which would disable the feature of bookmarking. A user would
have to re-pick the member's page each time.

I still think giving each member their own directory is the best way. It would only
have to contain the pages that can be bookmarked (index.php, aboutus.php, contactus.php, etc.)

01-08-2010, 01:41 PM
Thanks for your ideas mlseim. Much appreciated.

I think as my site is separated into xhtml valid content pages and design (css), I will make some further changes to the files and target just the css changes rather than my current web design styles in separate directories. That way it doesn't matter what is bookmarked it's only the css that will change, index, aboutus and contact pages remain the same.

Any future designs will be just a matter of css changes.

Thanks again.