Dynadot โ€” .com Transfer

PHP Problem

SpaceshipSpaceship
Watch

noswad

Established Member
Impact
1
Hi.

I use the following code to redirect someone back to the 'sign up' page if their desired username is taken.

PHP:
//make query to database
$sql ="SELECT * FROM $table_name WHERE username= '$_POST[username]'";
$result = @mysql_query($sql,$connection) or die(mysql_error());

//get the number of rows in the result set
$num = mysql_num_rows($result);

//checks it see if that username already exists
if ($num != 0){

header('Location:../index.php?id=');
exit;
}

As you can see, I want them to be redirected to the original sign up page ('index.php') to try again.

My problem is that when a new user tries to register, they may have been referred by a current user so may have clicked through the link e.g. .com/index.php?id=Chris (so 'Chris' has referred the new user).

I need the PHP code to make the page redirect back to index.php?id=Chris rather then just index.php?id=

Could anyone please please please help. I have tried my brains off to get get it to work but have had no luck.

If this helps, I use the following code on the 'index.php?id=Chris' page to catch the referer from the link and insert it into a hidden field on sign up...

PHP:
$id = trim($_GET['id']); 

if(preg_match("/([^a-z0-9]+)/i", $id)) 
{ 
    die('Invalid ID'); 
} 

echo '<input type="hidden" name="referredby" value="' . $id . '">';

Thanks.
 
Last edited:
0
•••
The views expressed on this page by users and staff are their own, not those of NamePros.
Unstoppable Domains โ€” AI StorefrontUnstoppable Domains โ€” AI Storefront
you can use session variables in order to transfer values between different pages.

i think you can use something like:

In your initial page,
PHP:
session_start();
$_SESSION['ref'] = $id; //or any other variable

and on later pages if you want to recall that value,
PHP:
session_start(); // you have to do this on every page you want to use sessions
$id = $_SESSION['ref'];

//testing
echo $id;


it is a bit late here, hope it works :)
 
0
•••
Hi thanks for replying.

I have edited the initial page and tried to do the following:

PHP:
//make query to database
$sql ="SELECT * FROM $table_name WHERE username= '$_POST[username]'";
$result = @mysql_query($sql,$connection) or die(mysql_error());

//get the number of rows in the result set
$num = mysql_num_rows($result);

//checks it see if that username already exists
if ($num != 0){
header('Location:../index.php?id=session_start(); $id = $_SESSION['ref'];');
exit;
}

Im not sure if this is along the right tracks or not?

I get the following error....

Parse error: parse error, unexpected T_VARIABLE in /homepages/33/d149588437/htdocs/mobiles/login/register.php on line 48
 
Last edited:
0
•••
PHP:
session_start();

$id = trim($_GET['id']); 

if(preg_match("/([^a-z0-9]+)/i", $id)) 
{ 
    die('Invalid ID'); 
} 

$_SESSION['ref'] = $id;
On your sign up page.

Then when you're checking...
PHP:
session_start();

//make query to database
$sql ="SELECT * FROM $table_name WHERE username= '$_POST[username]'";
$result = @mysql_query($sql,$connection) or die(mysql_error());

//get the number of rows in the result set
$num = mysql_num_rows($result);

//checks it see if that username already exists
if ($num != 0){
header('Location:../index.php?id=' . $_SESSION['ref']);
exit;
}
 
0
•••
PHP:
//make query to database
$sql ="SELECT * FROM $table_name WHERE username= '$_POST[username]'";
$result = @mysql_query($sql,$connection) or die(mysql_error());

//get the number of rows in the result set
$num = mysql_num_rows($result);

//checks it see if that username already exists
if ($num != 0){
session_start();
header("Location:../index.php?id=$_SESSION['ref']");
exit;
}

im not sure if thats going to help u or not..but try that
 
0
•••
Thanks to everyone who tried.

SecondVersion - you have helped me out brilliantly once again.

Thank you very much! :xf.love: :hehe:
 
0
•••
Just FYI be careful with HTTP headers. You should leave one space between 'location' and the URL like this:
header("Location: ../index.php?id=$_SESSION['ref']");

Some browsers may be more sensitive than others :]
 
0
•••
PHP is server side. So browsers never (or don't never) see the space.
 
0
•••
noswad said:
Thanks to everyone who tried.

SecondVersion - you have helped me out brilliantly once again.

Thank you very much! :xf.love: :hehe:
Lol, no problem :)
 
0
•••
Dan Friedman said:
PHP is server side. So browsers never (or don't never) see the space.
The headers are sent back to the browser so it's important that they be well-formed actually (some browsers are quite picky).
 
0
•••
Dynadot โ€” .com TransferDynadot โ€” .com Transfer
Appraise.net
Escrow.com
Spaceship
Rexus Domain
CryptoExchange.com
Domain Recover
CatchDoms
DomDB
NameFit
  • The sidebar remains visible by scrolling at a speed relative to the pageโ€™s height.
Back