3

Well, I'm trying to write a add friend script and he is working, but when my script (that is in a function) tries to search for $_GET['id'] to see what the id of the profile to make friend request, the script can't find because is a function that is in the profile.php and isn't directly in the file.

So I can made if I set the $_GET a session, but if I enter in a user profile and then entre in another and if I try to make a friend request to the first profile, the request is sent to the last one ( that is registered in the session )

So how can I make a $_GET "global"?

That's my code:

OTHER_PROFILE.PHP

    <?php include_once("includes/head.php"); ?>
    <?php require_once("includes/connect/connect.php"); ?>
    <?php require_once("includes/functions.php"); ?>
    <?php get_directory() ?>
    <?php login_validation() ?>
    <?php first_login(); ?>
    <?php count_logins(); ?>
    <?php get_shortcuts_menu_by_user(); ?>
    <?php include_once("includes/body.php"); ?>
    <?php 
    global $userid; 
    global $userid_profilee;
    $_SESSION['userid'] = $userid;
    $_SESSION['id'] = $_GET['id'];
    ?>

    <script src="alert.js" ></script>

    <?php 
    other_profile_main_photo();
    echo "<br><br>";
    ?>

    <?php include_once("includes/body_begin.php"); ?>
    <?php echo "<br><br>";?>
    <?php include("includes/footer.php"); ?>

FUNCTIONS.PHP (just the function part)

<?php require_once("/connect/connect.php"); ?>
<?php require_once("/jquery.php"); ?>
<?php
######### FRIEND REQUEST ########

function friend_request_send(){

global $db;

global $userid;
$userid = $_SESSION['userid'];
$userid_profilee = $_SESSION['id'];

$query_id_requester = "SELECT * FROM friend_requests";
$result_set1 = mysql_query($query_id_requester, $db)  or die(mysql_error()) ;

$query_id_requests = "SELECT friend_requests FROM members WHERE id=\"{$userid_profilee}\" ";
$result_set3 = mysql_query($query_id_requests, $db)  or die(mysql_error());

$query_id_requester_check = "SELECT * FROM friend_requests WHERE user_id=\"{$userid_profilee}\"";
$result_set4 = mysql_query($query_id_requester_check, $db);
$query_id_user_check = "SELECT * FROM friend_requests WHERE user_id_requester=\"{$userid}\"";
$result_set5 = mysql_query($query_id_user_check, $db);

## If already exists a friend request do this:
if ($id_requests = mysql_fetch_array($result_set3)){
if (($id_requester_check = mysql_fetch_array($result_set4)) && ($id_user_check = mysql_fetch_array($result_set5))){
echo "Ja fizeste pedido de Amizade!";
return ;
}
else
{
}
}



if ($id_requester = mysql_fetch_array($result_set1)){
$id_requests_1 = $id_requests['friend_requests'] + 1;
mysql_query("UPDATE members SET friend_requests=\"{$id_requests_1}\" WHERE id=\"{$userid_profilee}\"");
mysql_query("INSERT INTO friend_requests (user_id, user_id_requester) VALUES (\"{$userid_profilee}\", \"{$userid}\")");
echo "<div class=\"nNote nSuccess hideit\">
            <p><strong>SUCCESS: </strong>Pedido de Amizade enviado com sucesso!</p>
        </div>";
}

else
{
echo "Pedido de Amizade nao enviado!";
}
}    

Thats the error if I try to change $userid_profilee = $_SESSION['id']; to $userid_profilee = $_GET['id'];

Notice: Undefined index: id in C:\xampp\htdocs\includes\functions.php on line 13

Danny Beckett
  • 20,529
  • 24
  • 107
  • 134
  • 4
    Welcome to Stack Overflow! [Please, don't use `mysql_*` functions](http://stackoverflow.com/q/12859942/1190388) in new code. They are no longer maintained and are [officially deprecated](https://wiki.php.net/rfc/mysql_deprecation). See the red box? Learn about prepared statements instead, and use [tag:PDO] or [tag:MySQLi]. – hjpotter92 Apr 08 '13 at 00:38
  • 3
    `$_GET` is, by default, a _superglobal_. This means that it will be accessible within all functions without having to declare it as `global`. I believe your issue is somewhere else. – Colin M Apr 08 '13 at 00:41
  • Do a `vardump` of `$_SESSION` (in `functions.php`) and update your question with the result. – Kermit Apr 08 '13 at 14:57
  • @ColinMorelli I've tried to use $_GET and gived to me that error: Notice: Undefined index: id in C:\xampp\htdocs\includes\functions.php on line 13 The line is ($userid_profilee = $_GET['id'];) – Davide Gonçalves Apr 08 '13 at 21:32
  • @FreshPrinceOfSO array(5) { ["password"]=> string(7) "davide7" ["username"]=> string(11) "evolutiounx" ["login"]=> int(1) ["userid"]=> string(1) "1" ["id"]=> string(1) "3" } Thah's the var_dump... the "id" is the profile_id of the user that I want to add has friend, and if I enter to another profile that id is changed to the last profile that I've visited, so if I want to add the first one the friend request goes to the last profile that i've entered.. – Davide Gonçalves Apr 08 '13 at 21:43
  • @DavideGonçalves does the url have `?id=` near the end? – T0xicCode Apr 08 '13 at 22:35

1 Answers1

4

Let's point out a few things first, just to get an overall understanding of how the mentioned variables work!

By definition

$_GET and $_SESSION are superglobal, or automatic global, variables. This simply means that they are available in all scopes throughout a script. There is no need to do global $variable; to access them within functions or methods.

Having that said, those variables contain

$_GET associative array of variables passed to the current script via the URL parameters.

$_SESSION associative array containing session variables available to the current script. See the Session functions documentation for more information on how this is used.

In a web application, the only way to share information between users is to store that info, on the server! In your case you decided to go for a mysql a database. That is the most common choice that goes along with php!

Notice: The new standard interface to access a mysql database isn't through mysql_* functions anymore! this extension is deprecated already and will be removed within the next versions of php! Its preferible to go with mysqli_* functions instead or to use one of the two object oriented alternatives available mysqli and pdo

 

Ok, now let's head onto the add friendship example! Conceptually you need three pages only to accomplish your task, i wont go into detail too much because in the end the code is simple to write... but you need to understand sessions/get variables and how they are different, and still able to interact each other!

 

login.php This is the place where i will check the database for users existence and set logged user's profile id

session_start(); // this must be on any page after the login

// if : the user exists and the password matches then

// grab the $profile_id from the database and push it to the session array

$_SESSION['logged_profile_id'] = $profile_id; 

// else : error wrong username or password

 

other_profile.php This pages grabs the information of the people from the database, and addresses the "add friend" button or link

<?php session_start() ?>

<!-- includes + validation + loading person from the database and get info [id, name, status, etc] -->

<a href="addfriend.php?friend_profile_id=<?php echo $profile_id; ?>">Add Friend</a>


<!-- other includes and stuff -->

 

addfriend.php Here's where your $_GET variable is about to come... it stores the current reference to the friend!

session_start();

$_SESSION['logged_profile_id'] // this is the logged in user

$_GET['friend_profile_id'] // and this is the new friend that can be added to the database

 

As you can see global doesn't mean that you can pass the variable through different users at all! every user belongs to its own session within its own pages and the only way cross information, is to use some sort of database! it can be a file a dbms or whatever! it just need to be on the server and both users have to be able to read and write on it!

Remember : you have to call session_start on each page the session needs to be transported, furthermore session_start has to be called before anything is sent to the browser, so its best to place it at the very beginning of your page!

Community
  • 1
  • 1
ivoputzer
  • 6,427
  • 1
  • 25
  • 43