2

I currently have a login form and I want to pass the username across the URL. It is stored in a session variable.

I have this on the page after the login but it doesn't seem to be working.

$myusername = $_POST['myusername'];

I am looking for something like this:

page.php?myusername=example

The login page is a php page that connects to another php that checks the login details from mySQL database.

The main part from that is this (on the checklogin php page)

// username and password sent from form 
$myusername=$_POST['myusername']; 
$mypassword=$_POST['mypassword'];

On the following page, upon successful login I have this:

   <? 
    session_start();
header( 'Location: menu.php?myusername=$myusername' ) ;
    if(!session_is_registered(myusername)){
    header("location:index.php");
    }
    ?>

6 Answers 6

6

Edit, looking at your edits, I'd say the first section about the redirect looks like what you need

But I see you say

It is stored in a session variable.

Are you formatting a URL string from a php session and then trying to transmit get params somewhere? If so, did you try using the session API to get the username and then doing a php redirect?

 session_start();

 $username = $_SESSION['myusername'];

 // redirect
 header("http://your/sites/dir/page.php?myusername=$username");

But it looks like you're relying on the deprecated session_register and register_globals, based on this comment:

I am wanting to pass this throughout the application, I do have it saved as a session I think as this: session_register("myusername");

That's probably not using because register_globals has been turned off. You should leave it off. You want to avoid session_register and instead use the $_SESSION array.

Here's how you pull a "myusername" from a URL GET query

To extract the username from a URL you want to use $_GET, ie

 $myusername = $_GET['myusername'];

The part of the URL you're looking at is known as the "query string". The parameters formatted

?var=value&var2=value2...
are known as "GET" parameters. $_POST parameters are not transmitted in the URL itself. You can read more about HTTP GET vs POST, here.

To save the username in the session do

 session_start();
 // be sure to VALIDATE your inputs
 $_SESSION['myusername'] = $_GET['myusername']; // replace with post if appropriate
Sign up to request clarification or add additional context in comments.

15 Comments

I have changed it to a GET and it is still not appearing.. Any ideas?
@Xerting: We need to see some code to know what's wrong with your usage of $_GET.
I am working based on the feedback of a backend and it just wants to see parameters placed in the URL (?username=xxxx).
@xerting, see my redirect, you can insert the $username into the HTTP query.
I just tried it and it seems to not be working for some reason: I get a page cannot be displayed now ...page.php$myusername
|
2

$_POST arguments must come from a POSTed form. If you're passing arguments in the URL, that's a GET request, and the values will be available in $_GET.

$myusername = $_GET['myusername'];

Comments

2

Use $_GET instead:

$myusername = $_GET['myusername'];

Comments

2

page.php?myusername=example uses the GET method.

Try

$myusername = $_GET['myusername'];

Comments

1

You can use $_REQUEST to see all of the passed in variables. Try doing a print_r($_REQUEST) or the same for just your GET and POST. This will let you see exactly what values are being passed through and you can confirm whether or the value is assigned that you're trying to set the variable to.

Comments

0

Are you sure you don't have a redirect somewhere that is stripping url parameters? For example url_rewrite or any apache rewrite conditions could be dumping any query parameters and not actually passing anything to your PHP script.

Check your web server logs to ensure that the full URL (with query string) is actually being used to call your script.

Comments

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.