I'd like to test if a cookie exists, pretty basic, I know, but something doesn't want to work well. So, here is the pieces of code that are related:
<?php $quesID = the_ID()."GCQuestion";
if(isset($_COOKIE[$quesID])){
...
}
The test always fails, even when the cookie's name match the generated variable. Weird part is when I try to type the variable name by hand if(isset($_COOKIE["94GCQuestion"])), it works.
Question is : can we use a variable as a name for $_COOKIE ?
Assuming you're using Wordpress,
the_ID(): Displays the numeric ID of the current post. This tag must be within The Loop.
the_ID() is a template function and it will just print the ID, it doesn't return it.
To return the ID, use get_the_ID() instead.
<?php $quesID = get_the_ID()."GCQuestion";
if(isset($_COOKIE[$quesID])){
...
}
Related
I know there must already has this question been asked but I didn't find the answer cause I don't know how to search exactly what I want.
So, I wanna make such a link http://example.com/index.php?cat=about where I should insert some about info. But I don't know how to make a page with this URL containing ? symbol and other stuff, or how to edit that page then, where to edit and etc.
About Us
I've also made cat.php file but what next?
In your index.php file you can use the following:
if(isset($_GET['cat']) { // means if the user use the url with ?cat=something
echo "<1>About {$_GET['cat']}</h1>"; //print the about of the cat as html
}
OK, Suppose you've two PHP files. page_a.php & page_b.php.
page_a.php
<?php
echo "<a href='page_b.php?cat=about'>Click Me</a>";
page_b.php
<?php
print_r($_GET); // Show all GET contents
echo $_GET['cat']; // Show what content exist in 'cat' part of url
Hope, this will clear your doubt of how to send data in url from one page to another using GET mehtod.
To store variable data in the url, you can use the query string. This is the portion of the url that immediately follows the protocol, domain name and file path
The query string begins with ? and may contain one or more parameter parameter value pairs. The parameter and parameter value are separated by =. Each pair is separated by &.
Let's suppose you do development for a tourism company called Gi Tours which provides content in 3 different languages. Because site visitors will want to be able to select their preferred language, you can provide flag images that will indicate a certain language and wrap those flags with the appropriate hyperlinks. Rather than writing out the full language name, you can simply assign id numbers to represent each like this:
<?php
echo "<img src=\"img/ge.png\">";
echo "<img src=\"img/en.png\">";
echo "<img src=\"img/ru.png\">";
?>
If a visitor clicks the second flag which loads this url: https://www.gitours.ge/index.php?lang=2, your index.php code can be written to extract the value assigned to lang by using $_GET["lang"].
If you write in your index.php file:
<?php
echo $_GET["lang"];
?>
Your code will display:
2
Or on your index.php file, you can easily generate dynamic page content using $_GET array data.
<?php
if(isset($_GET["lang"])){ // this checks if lang exists as a parameter in the url
$lang=$_GET["lang"]){ // $lang will equal the value that follows lang=
}else{
$lang=1; // if there was no lang parameter, this sets the default value to 1
}
if($lang==2){
// show English content
}elseif($lang==3){
// show Russian content
}else{
// show Georgian content
}
?>
This is, of course, a simplified demonstration; other techniques can be used to interact with the $lang value.
#Lasha Palelashvili let suppose below example:
above you are sending only one input parameter cat through url to the index.php file at server side so when you send this data from url it will send by get method by default, if you want to get this url info(input parameter) at the php side so $_GET will help you to fetch that info $_GET is actually an array which stores your input parameter as key and input parameter's value as value for your case("index.php?cat=about") $_GET array will contain value like below:
$_GET = array("cat" => "about")
now at the server side you can easily get the value like:
//index.php
<?php
$cat = $_GET["cat"];
echo $cat;
?>
In my company's website, there's some code which I don't understand. It works fine. See:
extract($_REQUEST);
switch(#$task){
//Only for tracker app/ Later move to seperate api///
case "get_all_trackers":
$trackerObj = new trackerClass('trackerClass');
echo $service = $trackerObj->getAllTrackers($_POST);
break;
default:
//dsfsdfdsf
}
Now, you can see $task is not defined ("according to me"). But it somehow takes the string 'get_all_trackers' when requested. I came to know about this when I used PostMan and gave a parameter task=get_all_trackers and it worked. I even changed the name of $task to $somethingelse, and it still worked. How's it taking the value?
No. The one that's doing this is the extract() function. Based on your code, it assigns variables with names and values according to the $_REQUEST array key and value pairs.
Example:
Let's say the current URL is http://yourwebsite.com/yourpage.php?foo=bar.
Then $_REQUEST['foo'] would have a value of "bar". And if you use extract($_REQUEST), you would have a variable $foo, which has a value of "bar".
I have a very basic code.That involves sessions and array.
$abc=array();
$abc['name']=$db_name; //When i echo this one.It does echo the name i.e. 'Tilak'
$_SESSION['userpasswordmatch']=true;
$_SESSION['userpasswordmatch']['name']=$abc['name'];
echo $_SESSION['userpasswordmatch']['name'];
Now when i try to run the code.It shows me a warning stating cannot use a scalar value as an array as well as the echo part in the above code doesn't show anything.
Question :
1)Why am i seeing this error and what is the way to resolve this?
2)How to echo the above session.
Note :
1)$db_name is the name of the username that i get from the database.It works fine and i get the correct value in $db_name.
Update:
Same thing happens even if i do this :
$_SESSION['userpasswordmatch']=true;
$_SESSION['userpasswordmatch']['name']=$db_name;
echo $_SESSION['userpasswordmatch']['name'];
First you are defining $_SESSION['userpasswordmatch']=true;. true is a scalar value. Then $_SESSION['userpasswordmatch']['name']=$db_name; where you are treating $_SESSION['userpasswordmatch'] as an array.
You can simply do $_SESSION['userpasswordmatch']['name']=$db_name; and echo it.
No need for setting it to true. This would work -
$abc=array();
$abc['name']=$db_name; //When i echo this one.It does echo the name i.e. 'Tilak'
$_SESSION['userpasswordmatch']['name']=$abc['name'];
echo $_SESSION['userpasswordmatch']['name'];
Update
No need to define a blank array. When you are setting $_SESSION['userpasswordmatch']['name'] = $db_name;, $_SESSION['userpasswordmatch'] is already defined as an array.
$_SESSION['userpasswordmatch']['check']= true; // use this for that check
I am editing a template to try and add some conditional logic to my page.
The page template shows topics related to a user.
I want to add a piece of code which will grab the user name from the page we are viewing and then use that in a string for my conditional statements.
The code I have put together is as follows, but it breaks my page so I am doing something wrong.
<?php global
// I query the ID and try and set that to the $userID - I think I am doing this wrong, but when I echo the ID it gets the correct info.
$userID = get_queried_object()->ID;
// This is the string I create using the userID which should be from the query above
$memberstatus = get_user_meta($userID,'member_status',true);
?>
later on I use IF statements to use thsi result (which i know work) so i won't post them. My problem is trying to get the above to work.
Any help?
damm, looks like when I remove 'global' from the php it works! I thought global had to be in this...ah well
This might be a simple question but now i started working on GET method so I'm kinda new to it.
I have some images which are given some unique ids:
<imagetag>;
$b is the variable that stores the ID of the image.
Now I want to pass the $b variable value to imageid.php.
The link successfully opened imageid.php in my browser but the URL looks like imageid?id=. It does not display the id in the URL.
I also tried doing this:
echo $_GET['id']; // in my imageid.php
... but it's not printing the id that I just passed to imageid.php using the GET method? Why?
There isn't really enough information in your question to give a full answer, but have you checked that $b is really holding the image ID? Also, are you side "echo $var = $_GET['id'];" wouldn't just echo whether or not $var was assigned to? (i.e. false or true).
Please provide the full code so I can reply with a better answer.