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
Related
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'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])){
...
}
I ran into an issue that I am not able to figure out. I have a page that I want to add an image of "Verified" or "Unverified". The code I have is the following:
PHP
if($listing['priority']==0 {
$header->set('ver_status,'<img src="images/icon_unverified.png"/>');
} else {
$header->set('ver_status,'<img src="images/icon_verified.png"/>');
}
HTML
<?php echo $ver_status; ?>
For some reason when I run the page in my website, that variable comes up empty.
The database contains a listing table, and the table contains a priority field which the default value is 0. Unless a customer updates information, which automatically changes to priority field value to 1, all profiles state "unverified"
The $header is the variable assigned to the template
All priority codes at 0 should show the unverified image. All others 1-5 shold show the veriried image.
I did this in a different page and it worked fine. That code was:
if (config['language'] == 2) {
$header->set('language_flag','<img src="images/flags/Spanish.png" />');
} else {
$header->set('language_flag','<img src="images/flags/English.png" />');
}
where code 2 was Spanish and code 1 was English.
Is there something missing in my code that I am not seeing? I am not getting any error messages, just empty values.
You appear to be missing a close paren in your if statement. If that's copied and pasted directly out of your code, that could be causing issues - it should also be causing error messages, but you may have those turned off.
If it's just a typo from copying your code into SO, of course, this is unhelpful.
The variable comes up empty because there isn't one. If you want to retrieve an image through echoing a variable's value, simply type
if($listing['priority']===0){
$ver_status='<img src="images/icon_unverified.png">';
}else{
$ver_status='<img src="images/icon_verified.png">';
}
What you're doing in your code is setting an object's key, but when you're trying to get that same key, you're not referencing it correctly. echo $header->ver_status is how you should retrieve the value if you don't want to use a normal variable. I suggest reading http://php.net/manual/en/language.types.object.php
To note, you have a typo in your code as well. t('ver_status,' is an open string.
Also to note, there is no need for a slash in an tag unless you're using XML.
I have a call to a PHP script from my home page which I do like this:
echo 'Delete';
So it is pretty standard.
Then in my PHP I have this code:
<?php
// delete_problem
include '../connect.php'; // Here I have db connection settings
error_log ( ".......in delete problem");
$problem_id = mysql_real_escape_string($_GET["problem_id"]);
?>
And the last line where I try to get the problem_id is throwing the undefined index error. Any idea why?
Thanks!
Have you got an actual connection inside connect.php? Or does it just store variables and the like?
mysql_real_escape_string may be causing a problem as if a connection is not available it will fail.
Beyond that, try echoing out the contents of the GET variable. You can also check whether it exists by using (isset($_GET["problem_id"])).
For values coming from the user, always make sure they are present and possibly validate their format.
Use:
if (isset($_GET['problem_id']) && trim($_GET['problem_id']) != '') {
$problem_id = $_GET['problem_id'];
// make sure it is a number
} else {
echo "No problem id given!";
}
That warning appears because the $_GET array doesn't contain a value problem_id, most likely because it was not passed with the URL.
Bleh, all you people with the mysql_real_escape string...
Always check if a variable is set before you try and assign the value of it to another variable.
Also use (int) to cast!!
if (isset($_GET["problem_id"])){
$problem_id = (int) $_GET["problem_id"];
}
In my project, I am using pagination and I used these statements to get the page number detail:
$page=$this->uri->segment(3);
$this->session->set_userdata('page',$page);
echo $this->session->userdata('page');
When I print this session value in that page itself, I get the value correctly and when I click on the particular link and then print that data, I am getting the value like 'images'.
Why is this happening?
However, when I write the statements like
$page=$this->uri->segment(2);
$this->session->set_userdata('page',$page);
echo $this->session->userdata('page');
it's working fine.
My URL is: http://localhost/CI/user/index/4
I have the same problem with you, the session variable always get value 'images'. Then i realize that: if we made a session variable, then we must call redirect() directly.
This is my example:
$page=$this->uri->segment(3);
$this->session->set_userdata('page',$page);
redirect('control/function2');
Then, you can get continue your code in the 'function2'.
Solved the issue by writing another function, setting the session and redirecting to above function like this:
function set_session(){
$id=$this->uri->segment(3);
$this->session->set_userdata('id',$id);
redirect('set_uri_session');
}
function set_uri_session(){
$id=$this->uri->segment(3);
$this->session->set_userdata('id',$id);
echo $this->session->userdata('id');
}