Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8453417
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T11:46:09+00:00 2026-06-10T11:46:09+00:00

Possible Duplicate: global variables in php not working as expected I have a php

  • 0

Possible Duplicate:
global variables in php not working as expected

I have a php function that runs on every page of a website, it use a global variable, for example:

$var = "test";
function test() {
    global $var;
    echo $var;
}

This works fine when accessing /anyFile.php directly, but the website uses an htaccess file to rewrite urls, something like:

RewriteRule ^action/(.*)$ /index.php?action=$1 [L]

When an url is rewrited by the htaccess, the function doesn’t work, the $var is not set.

What could be this happening and how can I fix it? (I NEED to use “global”, otherwise I’d need to recode a lot of things.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T11:46:10+00:00Added an answer on June 10, 2026 at 11:46 am

    You need to use [QSA,L] instead of [L]:

    RewriteRule ^action/(.*)$ /index.php?action=$1 [QSA,L]
    

    QSA stands for Query String Append, and forwards the query string (the part after the ? in the url) to the PHP script.

    By the way, you should not use register_globals (deprecated as of PHP 5.3 and removed as of PHP 5.4), but use the $_GET superglobal instead.

    — edit —

    Following your comment below (you can’t modify the .htaccess), you’re out of luck. Your only solution is to parse the query string in the request URI, and use that as you would use the $_GET superglobal:

    $queryString = parse_url($_SERVER['REQUEST_URI'], PHP_URL_QUERY);
    parse_str($queryString, $query);
    echo $query['action'];
    

    I strongly advise you to get the .htaccess modified for you, though.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Are global variables in PHP considered bad practice? If so, why? global
Possible Duplicate: Android global variable I am working with an Android project. I have
Possible Duplicate: Using global vars within a function in PHP the way you do
Possible Duplicate: can't access global variables inside a usort function? I've experienced this problem
Possible Duplicate: Global Variables in Cocoa/Objective-C? problem with declare a global variable in objective
Possible Duplicate: Global variables in R ad the beginning of the file I wrote:
Possible Duplicate: Global variables in Java How to create a list of global variables
Possible Duplicate: Why global and static variables are initialized to their default values? What
Possible Duplicate: PHP global in functions Using something like this worries me: <? global
Possible Duplicate: What does jQuery.fn mean? I have some script on a page trying

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.