Jump to content

[SOLVED] $_GET['username'] in a link


runnerjp

Recommended Posts

how can i $_GET['username'] from going through a link?

 

if (isset($_GET['username'])) 
{
    	$username = mysql_real_escape_string($_GET['username']);
    	$query = "SELECT * FROM users WHERE Username = '$username' LIMIT 1"; 
    	if ($result = mysql_query($query)) 
{
      		if ((mysql_num_rows($result)) == 1) 
	{ 
        		$array = mysql_fetch_assoc($result);
        		$pemail = $array['Email'];
        		$puser = $array['Username'];
		     		} 
	else 
	{
        		echo "No users found with id $id<br />";
      		}
    	} 
else 
{
      		echo "Query failed<br />$sql<br />" . mysql_error();
    	}

  } 	else 
{
   		echo "No ID passed";
  	}
?>

 

this is what i use if i users goes by e.g mysite.com/members/username

Link to comment
https://forums.phpfreaks.com/topic/79086-solved-_getusername-in-a-link/
Share on other sites

What you're asking about is doing a mod_rewrite. Since you are passing in a username via a path structure (ie, "/members/<username>"), you cannot access it directly via $_GET. Are you using a mod_rewrite? You'll most likely need to parse your URL to get it instead.

thorpe i had last thread as solved...sorry will keep this 1 open...

 

well i have got my profile working but if i wanted the user to view there own profile by clicking a link saying view my profile on a page example.. wwww.mywebsite.com/members/index.php to get to the profile on www.mywebsite.com/members/profile.php

 

does that make better sence?

i think you should listen to thrope first but what the scrip do is.

 

<?php

//take the link from the current page
$link = $_SERVER['PHP_SELF'];

// this finds the last instance of slash / so no matter how mant slashes this will look for the last the one is added becuase it counts upto the last slash so +1 will add the last slash to the count
$pos = strrpos($link,'/')+1;

//this gets the length of characters in the link
$len = strlen($link);

// this take 3 paremeter the first is the normal link the second would be what you got form post and last what you got from length, check php.net for better descriptions.
substr($link,$pos,$len);


//the outcome of this is whatever is after the last /
?>

sorry finding it really hard to explain so here i go

 

ok so when a user wants to view a profile they would go to www.runningprofiles.com/members/thereusername

this would view the username entred profile.

 

but if a users logs into their own account and after updating their profile wants to view their own profile how ca i do it so they c get to this page www.runningprofiles.com/members/thereusername

Ok. Assuming your users are logged in using sessions, you can make your profile.php have some defaults.

 

<?php
  
  session_start();

  // see if a name was passed via the url.
  if (isset($_GET['username'])) {
    $username = mysql_real_escape_string($_GET['username']);
  } elseif (isset($_SESSION['username'])) {
    $username = $_SESSION['username'];
  } else {
    die("No profile to search for");
  }
  
  $query = "SELECT * FROM users WHERE Username = '$username' LIMIT 1"; 
  if ($result = mysql_query($query)) {
    if (mysql_num_rows($result)) { 
      $array = mysql_fetch_assoc($result);
      $pemail = $array['Email'];
      $puser = $array['Username'];
    } else {
      echo "No users found with id $id<br />";
    }
  } else {
    echo "Query failed<br />$sql<br />" . mysql_error();
  }

?>

hey can u help me lol ok i cant seem to add the 2 together..

 

with your code abouve with the get user session i have

 

function get_username ( $id )
{
	global $db;

	$query = "SELECT `Username` FROM `" . DBPREFIX . "users` WHERE `ID` = " . $db->qstr ( $id );

	if ( $db->RecordCount ( $query ) == 1 )
	{
		$row = $db->getRow ( $query );

		return $row->Username;
	}
	else {
		return FALSE;
	}
}

<?php
  
  session_start();

  // see if a name was passed via the url.
  if (isset($_GET['username'])) {
    $username = mysql_real_escape_string($_GET['username']);
  } elseif (isset($_SESSION['username'])) {
    $username = $_SESSION['username'];
  } else {
    die("No profile to search for");
  }
  
  $query = "SELECT * FROM users WHERE Username = '$username' LIMIT 1"; 
  if ($result = mysql_query($query)) {
    if (mysql_num_rows($result)) { 
      $array = mysql_fetch_assoc($result);
      $pemail = $array['Email'];
      $puser = $array['Username'];
    } else {
      echo "No users found with id $id<br />";
    }
  } else {
    echo "Query failed<br />$sql<br />" . mysql_error();
  }

?>

so its easy for u guys to see

<a href="<?php echo  . get_username ( $_SESSION['user_id'] ) . ?>">http://runningprofiles.com/members/<?php echo . get_username ( $_SESSION['user_id'] ) . ;?>

tried this and get error Parse error: syntax error, unexpected '.', expecting ',' or ';' in /home/runningp/public_html/members/index.php on line 6 yet i think i finished it right ?? 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.