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

  • SEARCH
  • Home
  • 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 227019
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T19:33:49+00:00 2026-05-11T19:33:49+00:00

It seems pretty simple but I can’t find a good way to do it.

  • 0

It seems pretty simple but I can’t find a good way to do it.

Say in the first page I create a variable

$myVariable = "Some text";

And the form’s action for that page is “Page2.php”. So in Page2.php, how can I have access to that variable? I know I can do it with sessions but I think it’s too much for a simple string, and I do only need to pass a simple string (a file name).

How can I achieve this?

Thanks!

  • 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-05-11T19:33:50+00:00Added an answer on May 11, 2026 at 7:33 pm

    HTML / HTTP is stateless, in other words, what you did / saw on the previous page, is completely unconnected with the current page. Except if you use something like sessions, cookies or GET / POST variables. Sessions and cookies are quite easy to use, with session being by far more secure than cookies. More secure, but not completely secure.

    Session:

    //On page 1
    $_SESSION['varname'] = $var_value;
    
    //On page 2
    $var_value = $_SESSION['varname'];
    

    Remember to run the session_start(); statement on both these pages before you try to access the $_SESSION array, and also before any output is sent to the browser.

    Cookie:

    //One page 1
    $_COOKIE['varname'] = $var_value;
    
    //On page 2
    $var_value = $_COOKIE['varname'];
    

    The big difference between sessions and cookies is that the value of the variable will be stored on the server if you’re using sessions, and on the client if you’re using cookies. I can’t think of any good reason to use cookies instead of sessions, except if you want data to persist between sessions, but even then it’s perhaps better to store it in a DB, and retrieve it based on a username or id.

    GET and POST

    You can add the variable in the link to the next page:

    <a href="page2.php?varname=<?php echo $var_value ?>">Page2</a>
    

    This will create a GET variable.

    Another way is to include a hidden field in a form that submits to page two:

    <form method="get" action="page2.php">
        <input type="hidden" name="varname" value="var_value">
        <input type="submit">
    </form>
    

    And then on page two:

    //Using GET
    $var_value = $_GET['varname'];
    
    //Using POST
    $var_value = $_POST['varname'];
    
    //Using GET, POST or COOKIE.
    $var_value = $_REQUEST['varname'];
    

    Just change the method for the form to post if you want to do it via post. Both are equally insecure, although GET is easier to hack.

    The fact that each new request is, except for session data, a totally new instance of the script caught me when I first started coding in PHP. Once you get used to it, it’s quite simple though.

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

Sidebar

Related Questions

What I want to do seems pretty simple, but I can't find any answers
Seems pretty simple but I can't get it to work. I have two divs
This seems pretty simple, but I can't seem to figure it out. I want
This seems like a pretty simple question but I can't seem to get an
I know this seems pretty basic, and it should be, but I can't find
I'm pretty sure there is a very simple solution here but I can't seem
This feels like it should be pretty simple, but not much seems to be
Im sure this is pretty simple but I just cant seem to find the
This should be pretty simple, but I can't seem to get it. I have
The task seems fairly simple but I don't know what's the best way to

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.