I'm trying to set a session variable to record when people have voted but PHP is flat out refusing to set it. The code is as follows:
elseif (isset($_GET['group']) && isset($_GET['vote']))
{
include_once(_INC.'otherheader.php');
groupVotePage($group, $vote);
$_SESSION[$group] = '1';
echo $_SESSION[$group];
}
Nothing. The function groupVotePage adds the vote to the database and echoes a thanks message. $group is the name of the group being voted for. I have session_start(); at the top of the page and have tried to declare the variable inside the function called as well, putting session_start(); everywhere. Session variables are used elsewhere on the site so I know it's not a server issue, and it's the same on all browsers I tried.
Declaring the session var inside the function works but only within the function - it doesn't go global.
if(!isset($_SESSION[$group])) {
$totalVote=$totalVote+$vote;
$totalNumVotes=$totalNumVotes+1;
$totalRating=round($totalVote/$totalNumVotes);
$totalScore=$totalVote*$totalNumVotes;
...db stuff...
$mysqli->query($query);
echo'Thanks for voting!';
}
else {
echo'You have already voted for this group!';
}
Maybe you can use PHP error reporting code to figure out where you going wrong
// Report all errors
error_reporting(E_ALL);
Okay fixed it, the < !DOCTYPE HTML> section was before the session_start();, when I put it after the code worked.
Related
I have a function on my page to login it works like this:
function entrarSistema($email,$senha){
if(isset($email) and (autentica($email,$senha)!=false)){
$mysqli = connect_db();
$result = mysqli_query($mysqli,"SELECT ID FROM px_user WHERE email = '$email'");
$id = mysqli_fetch_array($result);
$_SESSION['nome'] = autentica($email,$senha);
$_SESSION['email'] = $email;
$_SESSION['password'] = $senha;
$_SESSION['ID'] = $id[0];
$_SESSION['logado'] = true;
}
else{
if(check_double($email)==1){
setCodeAlerta(1);
echo $_SESSION['status'];
}
else {
setCodeAlerta(2);
}
}
}
This function works fine, and the Session variables are set when i call the function setCodeAlerta(), the Session variable i've declared wont work. Notice that this 2 functions are on the SAME file Here is the function:
function setCodeAlerta($numeroCodigo){
$_SESSION['status'] = $numeroCodigo;
}
My index.php has all the pages included, and i use url_rewrite to add the piece of codes that i need, and has this on its very top:
if( !isset($_SESSION) ){ session_start(); }
Oddly enough, if i call directly a file named test.php with this code:
<?php
setCodeAlerta(2);
?>
The variable is set fine, and everything works well.
Thanks in advance.
I'd add a comment, but my rep isn't high enough yet. Out of curiosity are the other session vars available at that point that you are setting? -- I ask because I'm running your code (stripped down) and it's returning OK. I'm just thinking there's something more to this than just that. Are you calling session_start() twice anywhere in an include or anything?
I got that. Everything was absolutely fine with my code, i just did a conditional, where my code would never really go to wrong username/password, hence, not accessing my function (wich is working fine). After removing the second part (after the and) part of this conditional, my code worked just fine.
if (isset($_POST['submitLogin']) and autentica($_POST['emailLogin'],$_POST['passwordLogin']){
entrarSistema($_POST['emailLogin'],$_POST['passwordLogin']);
}
I normally ask questions and find my answer minutes after that, i think thats a bad habit of mine. Even so, i spent the last 5 hours debugging it.
Thanks!
In this chunk of code it was previously designed to use the session_id. I am trying to convert from using the session_id to using a User ID that is retrieved from the database. I'm not sure what I did wrong but the function is not returning the variable. Any suggestions would be appreciated.
protected function get_user_id() {
//previous code used the session id
//#session_start();
//return session_id();
// New code to use User ID instead of session_id
// Connecting to the database
include ("../../../admin/includes/connect.php");
// Let's get the user ID from the database for use with the widget
$user_id_query = "SELECT nonadmin_user_id FROM `nonadmin_user_login` WHERE email = '$_SESSION[email]'";
$run_query = mysqli_query($conn, $user_id_query);
while($row=mysqli_fetch_array($run_query)){
// Create variable for the user's id
$nonadmin_user_id = $row['nonadmin_user_id']; }
return $nonadmin_user_id;
}
// This function needs to use the variable $nonadmin_user_id
protected function get_user_path() {
if ($this->options['user_dirs']) {
return $this->get_user_id().'/';
}
return '';
}
"Fred you're the man! It was the session. I removed the comment out from in front of the session start and now it works perfect. What baffles me on this is I was under the impression that if you start a session in a file and then include other files the included files did not require the session to be started."
The session needs to be started in order for the session array to be recognized and passed successfully in your query.
Plus, session_start(); is required to be resident inside all files using sessions.
http://php.net/manual/en/function.session-start.php
Add error reporting to the top of your file(s) which will help find errors.
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
// rest of your code
Sidenote: Error reporting should only be done in staging, and never production.
For example:
If anyone fails at the login function (for example: enters wrong password) on my webpage, i want to show an error-message at the webpage. My idea was like that:
if(doLogin()) {
//....
}else {
$GLOBAL['errorLogin'] = "Wrong Userdata";
}
and then echo the global-variable in the .html.
But i searched also for this topic and found only this method, but everyone had used the $_SESSION variable for this instead of $GLOBAL.
Is my variant with the $GLOBAL varible wrong or bad practise?
And why use $_SESSION for a error-message, if i only echo the message one time and don't need it in the next request?
I think you mean $GLOBALS (notice the s) which is a suber global variable and therefore can be accessed from anywhere in the PHP script (also from within functions or methods).
There is nothing wrong about that.
I don't think that you should use the $_SESSION variable for that, because the user needs to see the error message only one time. In your case, and in most cases, that's why it might make no sense to store it in a session.
Personally, I just would use a custom errorMessage-Array, like that:
//store all Error Messages in one array.
$errorMessages = array();
if(doLogin()) {
//....
}else {
$errorMessages["Login"] = "Wrong Userdata";
}
//...
foreach($errorMessages as $key=>$message){
echo $key.": ".$message."<br>";
}
I'm trying to work with Zend Sessions and after failing for a while, I tried basic counter example:
$defaultData = new Zend_Session_Namespace('language');
if(isset($defaultData->counter))
{
$defaultData->counter = 1;
} else {
$defaultData->counter++;
}
echo $defaultData->counter;
But on each page refresh I get the value 1. I'm calling Zen_Session::start in Bootstrap autoloader. What are the possible ways to debug, solve this problem?
Your if-else is the wrong way around. Now, if there is a session variable, you assign the value '1'. So, change statements for if and else and you will be fine.
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"];
}