0

Raising a three years old question. How to get a user id from one php file and retrieve it in another php file

From my own experience I tried to get an id from one file to execute it in another to retrieve some data from another website. Whenever the page loads I could see the value in url on my browser but the value is returning empty when I tried to use it. Kindly check below what I have tried and suggest if there is a way I could do it with javascript or jquery or just do it with only PHP.

a.php

<?php

$aa = b.php;

$bar = 1234;

echo “<a href=$aa?$bar target=new window>”Click here”</a>”;

?>

b.php

<?php

$foo = $_GET[$bar];

// this one returns empty. But bar is seen on the url as ?bar
echo $bar;
?>
Charlie Bamford
  • 1,268
  • 5
  • 18
Dlaw
  • 131
  • 2
  • 14
  • 1
    You've forgot to add the `bar` variable name in url: `echo "Click here";` and always enclose tag atributes into double quotes (escape them when needed) – Triby Jun 05 '20 at 17:26
  • I didn't know having it inside a variable again was important. – Dlaw Jun 05 '20 at 18:04
  • 1
    Yes, it's important, other way you'd had a "variable with no value" (1) or "value assigned to no variable" (2) in your URL. 1) Is the way the browser will parse the URL, 2) Maybe is the user/programmer point of view – Triby Jun 05 '20 at 18:18

2 Answers2

1

How u echo the anchor tag doesn't seems good to me, maybe more like:

echo '<a href="'.$aa.'?bar='.$bar.'" target="new window">Click here</a>';

You can access the value like:

$_GET['bar']

And you have to echo $foo and not $bar.

Urudin
  • 286
  • 2
  • 14
  • Why is it better to concatenate instead of parsing variables using double quotes? – Triby Jun 05 '20 at 18:24
  • I've always done like this, and I think in html there's must be "" in attributes, what you can't do if the whole string in double quotes. – Urudin Jun 05 '20 at 19:15
  • Yes, you can do it just by escaping double quotes: `echo "Click here";` I like to parse variables instead of concatenation, but who knows wich method is better? – Triby Jun 05 '20 at 20:15
  • Yes you are right. I'm used to concatenation, seems cleaner for me, but it's a matter of taste :) – Urudin Jun 06 '20 at 11:37
1

Check the comments added... This is how it's done...

<?php
//a.php

$b_php = "b.php"; //file name
$parm = "?bar="; //parameter
$parm_data = "1234"; //DATA
echo "<a href='$b_php$parm$parm_data' target='_blank'>Click here</a>";
?>

//b.php

<?php 
if(isset($_GET["bar"])){ //check if the variable parameter is set or not
$foo = $_GET["bar"]; // $_GET["$bar"] only works if variable exists in the file
echo $foo; //echo $foo
}else{
echo "Empty!";
}
?>

It's returning empty because you are trying to echo Variable $bar but it doesn't exist. Also look in to GET & POST forms for data from HTML or JS or Jquery to PHP.

c0de b0unty
  • 108
  • 9
  • Nice answer, it'd be much better with indentation and, maybe, **1)** Separate codes for a.php and b.php **2)** adding and option and explanation to get the variable using ternary operator. – Triby Jun 05 '20 at 18:22