Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 11-27-2012, 03:58 PM   PM User | #1
Johnb21
New Coder

 
Join Date: Sep 2010
Posts: 63
Thanks: 1
Thanked 2 Times in 2 Posts
Johnb21 is an unknown quantity at this point
Problems with $_GET[]

I have 2 versions of this code... one WITH Sessions and one without. The one without the sessions being used works just fine but the one with sessions is having problems.

I have a url:
www.example.com/register.php?refid=1234567

I am trying to get the refid from the url and save it to a variable. I want to do this when a "Submit" button is clicked to register after filling a form out.

If I have code that says

Code:
if(isset($_POST['submit']))
{
//RUN THIS CODE
}
when I fill the form out and click submit, the page refreshes and the url becomes:

www.example.com/register.php

This works:

Code:
 if(!empty($_GET["refid"])){
     $refered_by = $_GET['refid'];
}
if(isset($_POST['submit']))
{
//RUN THIS CODE
}
but this does not:

Code:
if(isset($_POST['submit']))
{
   if(!empty($_GET["refid"])){
     $refered_by = $_GET['refid'];
   }
}
Even with the first example that DOES work... if I try to reference that $refered_by variable inside the code block that validates the submit button was clicked... it tells me that the $_GET['refid'] is undefined.

Is there any reason the ?ref=123456 is being removed from the url when the user clicks the submit/sign up button? Is this causing $_GET['refid'] to be undefined? Lastly... even if that's the case, why isn't $refered_by saving the variable if I establish it before the submit code block?

Any help will be appreciated so I can not only fix the code but also understand what is happening here.
Johnb21 is offline   Reply With Quote
Old 11-27-2012, 04:29 PM   PM User | #2
Thyrosis
New Coder

 
Join Date: Nov 2012
Posts: 72
Thanks: 4
Thanked 11 Times in 11 Posts
Thyrosis is on a distinguished road
What is the action your form points to? Is the ?refid=123456 passed there as well?

Also check the source code before pressing the submit button to double check where the form is going (action).
Thyrosis is offline   Reply With Quote
Old 11-27-2012, 04:57 PM   PM User | #3
Johnb21
New Coder

 
Join Date: Sep 2010
Posts: 63
Thanks: 1
Thanked 2 Times in 2 Posts
Johnb21 is an unknown quantity at this point
With the url: www.example.com/register.php?refid=12345

Here is my code for register.php

Code:
<?php
	require 'core.php';
	require 'connect.php';
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="description" content=""/>
<meta name="keywords" content=""/>
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<title>Register</title>
<link rel="stylesheet" href="style.css" type="text/css" media="screen" />

	<style type="text/css">
	body {

	}
	</style>
	
</head>
<body>
	
	<?php 
		if (!loggedin()){
		
			if(isset($_POST['firstname']) && isset($_POST['lastname']) && isset($_POST['username']) && isset($_POST['email']) && isset($_POST['password']) && isset($_POST['verify_password']))
			{
				$firstname = $_POST['firstname'];
				$lastname = $_POST['lastname'];
				$username = $_POST['username'];
				$email = $_POST['email'];
				$password = $_POST['password'];
				$password_verified = $_POST['verify_password'];
				
				$min_pass_len=6;
				$max_pass_len = 16;
				
				$userId = uniqid();

				if(!empty($firstname)&&!empty($lastname) && !empty($username)&&!empty($email)&&!empty($password)&&!empty($password_verified)){
					
					$fullname = $firstname . ' ' . $lastname;
					
					if($password <> $password_verified){
						echo "Passwords do not match!";
					}else{
						if(strlen($password) < $min_pass_len || strlen($password) > 16){
							echo 'Your password must be between 6 and 16 characters.';
						}
						else
						{
							
							$password = md5($password);
							
							$query = "SELECT username FROM users WHERE username='$username'";
							$query_run = mysql_query($query);
							
								if(mysql_num_rows($query_run) == 1){
									echo "The username \"$username\" is taken.";
								}
								else
								{
										
										if(isset($_GET['refid'])){
											$refered_by = $_GET['refid'];
										}
										
									echo "REF BY: $refered_by";									
									
										
								}				
						}
					}
					
				}else{
					echo "All fields are required.";
				}
			}				
		?>
			
			<form action="<?php echo $_SERVER['SCRIPT_NAME'];?>" method="post">
		<p>
			<label for="firstname">First Name</label><br />
			<input type="text" name="firstname" />
		</p>
		
		<p>
			<label for="lastname">Last Name</label><br />
			<input type="text" name="lastname"  />
		</p>
		
		<p>
			<label for="username">Username</label><br />
			<input type="text" name="username" />
		</p>
		
		<p>
			<label for="email">Email</label><br />
			<input type="text" name="email"  />
		</p>
		
		<p>
			<label for="password">Password</label><br />
			<input type="password" name="password"  />
		</p>
		
		<p>
			<label for="verify_password">Verify Password</label><br />
			<input type="password" name="verify_password"  />
		</p>
		
		<p><input type="submit" name="submit" value="Register &raquo;" /><input type="reset" name="reset" value="Reset" /></p>
		
	</form>
			
	<?php
		}else if(loggedin()){
			
		}
	?>
			
		
</body>
</html>
When calling the $_GET function before the if(!loggedin()){} code, it reads it just fine. I mentioned that before though... and here is what the loggedin() function looks like in an external page (yes it is included in the register.php page):

Code:
function loggedin(){
	
		if(isset($_SESSION['user_id'])&&!empty($_SESSION['user_id'])){
			return true;	
		}else{
			return false;
		}
	}
As I stated before, when the submit button is clicked... the page is refreshed with no refid in the URL. I am not sure if that is what is causing undefined but I wouldn't think that should matter because $_GET should happen before the "refresh"
Johnb21 is offline   Reply With Quote
Old 11-27-2012, 07:35 PM   PM User | #4
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,648
Thanks: 4
Thanked 2,450 Times in 2,419 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
Nor will it.
Querystrings do not persist unless you specifically set them. As mentioned, if you want to pass the querystring through, you need to add that into the action attribute of the form element. You can pull that off using the $_SERVER['QUERY_STRING'] (or in a few other ways as well), or reconstruct the querystring from the $_GET or explicitly what you are requiring. But if you want to end up with the querystring in this page after submit, you must add that to the action.
Fou-Lu is offline   Reply With Quote
Old 11-27-2012, 10:27 PM   PM User | #5
Junsee
New Coder

 
Join Date: Jun 2012
Posts: 42
Thanks: 4
Thanked 5 Times in 5 Posts
Junsee is an unknown quantity at this point
www.example.com/register.php?refid=1234567

Save to Session
PHP Code:
$_SESSION['Save'] = $_GET['refid']; 
but always remember to initialise session code
PHP Code:
session_start(); 
so at the top of every page, that uses or passes sessions, the first things should be:
PHP Code:
<?php
session_start
();

...
?>
EDIT:
So when there is a $_GET['refid'] use if
PHP Code:
if($_GET['refid']){
$_SESSION['Save'] = $_GET['refid'];


Last edited by Junsee; 11-27-2012 at 10:33 PM.. Reason: update
Junsee is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 07:56 PM.


Advertisement
Log in to turn off these ads.