0

I need to add a variable of the username to all of my URL's when the user is logged in.

So for example it would be:

  • www.example.com/?user=user1
  • www.example.com/category1/product1/?user=user1

The reason I need to add the variable is for sending the page to a PDF service as each user has a unique style to their template.

I have my variable set but how can I rewrite the page URL to include the variable if it isn't already included on the URL using php?

5
  • Why do you want to do it via php? Shouldn't you try some webserver configuration? Commented Jul 19, 2013 at 15:23
  • 2
    Is the PHP application that has the user's credentials the same as the one that generates the PDF? If so, you might consider using a session object instead of URL injection. Commented Jul 19, 2013 at 15:24
  • 2
    If you absolutely must use php, could you use a HTTP 301 redirect? Commented Jul 19, 2013 at 15:25
  • @PaulProgrammer unfortunately not, using a wordpress install which generates user credentials and and external service for PDFing Commented Jul 19, 2013 at 15:26
  • Is the url with the variable supposed to trigger a redirect? Commented Jul 19, 2013 at 15:32

2 Answers 2

1

Apache can't know if the user is logged in, it concerns your PHP code. In this case, I think the best solution is to add a SESSION variable like :

$_SESSION['user'] = 'user1';

And instead of check if the user parameter exists, you will have to check if the $_SESSION['user'] exists. That will be easier and safer.

Sign up to request clarification or add additional context in comments.

Comments

0

What you're looking for isn't actually php, it's an apache module in your .htaccess file. This is the best blog I've seen on the subject: http://www.addedbytes.com/articles/for-beginners/url-rewriting-for-beginners/

3 Comments

This only works if apache knows about the user credentials, which it may not if user management is done in the application.
why? "?user=user1" is just a string in the initial url that remains unchanged in the final url. It can be simply regexed like any other part of the url
I read that OP wants to add the ?user=user1 to the URL, which isn't there when it appears in Apache.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.