PHP ECHO to pass two Var's via URL - php

echo "<li><a href=\"#\" onclick='load_page_device(\"mg-device.php?device=device_name&sensor=$p1_url\")'>".$row['p1_name']."</a></li>";
Alright I simply trying to pass two variables in a url via a php echo statement. It's simply not working.
I am then using
$device_name = $_GET['device'];
$sensor_name = $_GET['sensor'];
I have tried also to use & instead of & only. The first variable always seems to get passed but the second one never does. Any help is greatly appreciated I have been trying to get this to work for hours now. Thanks!

try
echo '<li>'.$row['p1_name'].'</li>';
you need to first check that $_GET['device'] , $_GET['sensor'] is set
try
if(isset($_GET['device']) && isset($_GET['sensor'])){
//stuff
}else{
//other stuff
}

Related

PHP Session Variable get lost and session id is changing on every request

i found many answers about that problem but nothing solved my problem - so i want to show you my code and hope that someone can find the mistake..
I have a standard HTML formular that gives some data with POST to the next .php file where i get it and save it into session-variables. I use the session variables about 2 reasons:
if someone reloads the page, it should show the same information as before.
I need the variables in upcoming php files.
Here is the code:
session_start();
// Handle Variables on post and reloaded-page
if(isset($_POST["locId"]) && isset($_POST["dateId"]) )
{
$locId = htmlspecialchars($_POST["locId"]);
$dateId = htmlspecialchars($_POST["dateId"]);
$_SESSION["locId"] = $locId;
$_SESSION["dateId"] = $dateId;
echo "Session variables are set: locId = " . $_SESSION["locId"] . " dateId = " . $_SESSION["dateId"];
} elseif(isset($_SESSION["locId"]) && isset($_SESSION["dateId"])) {
echo "get it from session";
$locId = $_SESSION["locId"];
$dateId = $_SESSIOn["dateId"];
} else {
$load_error = 1;
$status = "alert alert-danger";
$message = "shit, no variables here";
}
The frist call works fine - session variables are set and the echo gives the right values. After reloading the page i get the echo "get it from session" but my variables have no values.
i also checked my session_id() on first call and reload.. they are NOT the same.
I testet a simple test.php file where i start a session with a variable and ask for the variable in the next file. It works fine :-/
Its just a problem with my code above. I think my webserver is handling right. But what reasons are there for chaging a session id and losing session-variable values?
Damn! To write correct is everything ...
I found my mistake.
Look at the code in my question. The second session-variable is $_SESSIOn["dateId"].. the n is lowercase! If i write it correctly and complete in UPPERCASE it is working.
Also the session_id is not chaging anymore and i can output the session_id() as much as is want.. but one mistake in $_SESSIOn changes everything. New session_id on every call, ... strange.
Learned something again :-) Thanks to everybody for the answers and your time! I hope i can help you in the future
Well, your mistake is quite easy to find. In fact, your code works perfectly. But look at this part:
echo "get it from session";
$locId = $_SESSION["locId"];
$dateId = $_SESSIOn["dateId"];
Well, you asign the session values to two variables, but in fact, you simply missed to output them anywhere. Thats why you get "get it from session" but then is displays nothing, you need to echo them.
Simply add an echo and it will display your vars perfectly :)
echo "get it from session";
$locId = $_SESSION["locId"];
$dateId = $_SESSIOn["dateId"];
echo $locId;
echo $dateId;
Try this:
session_id();
session_start();

echo function call 2 variables

Ok so I have the code for a form that is called and works but it needs two varibles grabbed from the string of a url. I have the first and the second is the same for what im doing on any page that I am creating which is alot. Here is the code at the url: collabedit.com/9g99j
Question if Get <?php echo $_GET['id']; ?> is grabbing my id string from the url how do I use this in the echo of my function I just defined at the bottom of the code? Instead of having this: echo DescriptionGet(1256124, 50874); can someone tell me how to put something like this: echo DescriptionGet(1256124, $id);
This would make it so i dont' have to enter that id value for every page I want to create.
Thanks,
Thanks everyone for your replies and I was able to figure it out on my own and actually used exactly what the first reply was.
Now I have a new question about this function. How do I make it grab the image from that same page its grabbing the form code from? I can't figure this part out and its keeping me from doing mass automation for this site.
Anyone help?
Try this:
$id = $_GET['id'];
echo DescriptionGet(1256124, $id);
You can change your function definition from:
function DescriptionGet($c, $id)
to
function DescriptionGet($c, $id=50874)
Each time when you will call DescriptionGet($c) it will behave as you passed $id=50874 but also if you need you can call DescriptionGet($c, 20) and $id in the function will be set to 20.
And in case you want to simple use $_GET['id'] as function parameter you can simple run
echo DescriptionGet(1256124, intval($_GET['id']));
you don't even need to use extra variable.

php: failing when including a file that is defined in an array

I want to include a file, based on a value I get from my database. Inside class page in page.php I have this function to get the data I want :
function getPage(){
$page=array(
'title'=>$this->title,
'content'=>$this->content,
'module'=>$this->module
);
return $page;
}
And in pageview.php I call it like this : $elements=$page->getPage();
The function works just fine and I get my content , which I can manipulate like this for example(I know heredox is not the way to go but please ignore it, it's not the problem) :
echo <<<EOT
<div class='row-fluid'>
<H1> title:$elements[title]</H1></br>
$elements[content]
$elements[module]
</div>
EOT;
Now here comes the problem. I have a function called includeModule, which is like this :
function includeModule($page){
$page=strtolower($page);
if(file_exists("modules/".$page."php")) include("/modules/".$page."php");
else Echo "No such Module Exists :".$page." </br>";
}
Now lets say I want to include a page named "tax.php". If I use include("/modules/tax.php)"; it works just fine. If I try though to use include("/modules/".$elements[module].".php)"; it does nothing ( an yes $elements[module] does contain only the word "tax" ).
I even tried assigning the value of $elements[module] to another variable but nothing.
The strangest part of all is that if I try to use my function ( includeModule), even if I manually set the $page variable to "tax", it still doe not include anything and says that the file does not exist ( even though it does) ?
Any help on this ?
edit: i already tried removing the / but if i do it includes nothing again
edit: i've change the function a litle so i can get some feedback
if(file_exists("modules/".$page.".php")){
echo "<p>file exists</p>";
include("modules/".$page."**.**php");
}else{
echo getcwd();
Echo "<p>No such Module Exists :".$page."</p>";
}
OK solved. thanks for all the answers. By my mistake the trailing slash as well as the dot right before the php extension were left out . thanks for helping :)
if i use include("modules/tax.php)"; it works just fine.
In your code, you're using
include("/modules/".$page."php");
^-- Note the leading /
Try removing the /
You are seeing if "modules/".$page."php" exists but trying to include "/modules/".$page."php" which are probably not the same things (notice the leading / on the latter).
if(file_exists("modules/".$page."php")) include("modules/".$page."php");
OK ive solved it thanks guys:) each answer was helpfull :) ( the answer was a combination of your answers+a litle more atttention needed from me . I forgot the . right before the php extension .. lolz.. guess when you are tired you have to take a litle break off word in order to be able to concetrate right ). thanks again guys :)

Getting value from session PHP

I am not sure why the variable username is not being returned in the session. When the user logs in, I start the session:
$username = trim($_POST['username']);
if(!isset($_SESSION)){ session_start(); }
$_SESSION[$this->GetLoginSessionVar()] = $username;
On the user's welcome page, when I run the echo command, I see the proper variable being returned. But I'm not sure why the return statement isn't working. I have the following in my PHP file:
function UserName()
{
return isset($_SESSION['name_of_user']) ? $_SESSION['name_of_user'] : "Unknown User" ;
//echo $_SESSION['name_of_user'];
}
In my html, I have:
Welcome back <?PHP $fgmembersite->UserName(); ?>!
I also checked the session ID, and it's also being generated properly.
Can you please help me understand what I'm doing wrong?
Is fgmembersite an object and have it the function called UserName ?
If yes, you simply miss an echo
<?PHP echo $fgmembersite->UserName(); ?>
You must add echo or print so should look like this;
<?PHP echo $fgmembersite->UserName(); ?>
You need to print out your variable. Use
Echo or print
Possibly you should add output:
<?php print $fgmembersite->UserName(); ?>
If you are using the script I think you are using, you need to look through fg_membersite.php at the line that says:
function CheckLoginInDB($username,$password)
whithin that line you should have a MySQL statement:
$qry = "SELECT etc...
When I tried to add UserAvatar I was able to do that by adding it to that MySQL string.
On a side note, I too am having trouble with adding UserName, and for the life of me I can't figure out why it would work any different than my previous workaround, yet somehow it is, but I am still convinced something in that file will do the trick eventually.
Edited:
Ok i got it, just do this:
echo $fgmembersite->UserName($username);
The username will pop right out. I have no idea why, i don't know enough php to explain it, but i can only assume this will get you going.

Curl Problem, I just don't get it

SOLVED: var_dump revealed that that the issue was of & and & .
Also I would still like to know why curl fetched a page with missing divs. Would anyone be kind enough to explain ?
have been at this problem for 1.5hours now. Narrowed down to something that just doesn't make sense to me. Please help . I am fetching a page using curl.
The problem is that when the following code is used:
a lot of div tags from the navigation to the left are missing from the fetched page,
$pageToParse = "http://www.themarketgrocer.com.au{$arrayLeftBar[$i]} " ;
;
//fetch target page
$curlFetched = http_get($pageToParse, "http://www.google.com") ; //fetched using a curl function
echo $curlFetched['FILE'];
the value of $arrayLeftBar[$i] is fetched from an html page using simplehtmldom. as echoed:
"/index.php?option=com_content&view=category&layout=blog&id=37&Itemid=92"
HOWEVER if
$pageToParse = "http://www.themarketgrocer.com.au{$arrayLeftBar[$i]} " ;
is changed to : (basically manually appending the value of $arrayLeftBar[$i] )
$pageToParse = "http://www.themarketgrocer.com.au/index.php?option=com_content&view=category&layout=blog&id=37&Itemid=92" ;
then the complete page is fetched.
WHY ?? I have echoed the values and they are identical. What silly little thing am I missing?
Edit:
THis is how the $arrayLeftBar is being populated: I am pretty sure the problem is here . Coz if I manually create a simple array. Things work.
foreach ($sublevelLinks as $link)
{
$arrayLeftBar[] = $link->href ;
}
On the $arrayLeftBar[$i] value use urldecode
$query = urldecode($arrayLeftBar[$i]);
$pageToParse = "http://www.themarketgrocer.com.au{$query}" ;
SOLVED: var_dump revealed that that the issue was of & and & .
html_entity_decode($arrayLeftBar[$i]) did the necessary conversion

Categories