To understand session overwriting better with two session initiations [closed] - php

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
How can you see the assigned value of the following SESSION variable?
I run the following after start_session()
$_SESSION['login']['email'] = "ntohuh";
I get after printing with print_r($_SESSION);
( [login] => Array ( [email] => )
This question is based on this thread.

The value shows up for me. This is what I did, if it helps:
# This empties $_SESSION
$_SESSION = array();
session_start();
$_SESSION['login']['email'] = "ntohuh";
echo '<pre>';
print_r($_SESSION);
echo '</pre>';

Reply to this question.
Source of the problem
I found out that the problem was in my handle_login_session.php -file at the very beginning of my index.php. I had this there
The very beginning of my index.php in the file handle_login_session.php
if( $_SESSION['login']['logged_in'] == false ){
$random_number = rand(1,100000);
$session_id = session_id($random_number);
$_SESSION['login']['email'] = ''; // problem here
}
I did not think that this could be a problem when making this because I have the following at the very end of my file
The end of my index.php in the file handle_registration.php
$email = $_POST['login']['email'];
$_SESSION['login']['email'] = "ntohuh"; // not effective
I had the idea that the code later in my index.php will overwrite the code before it. However, this is not the case here.
Explanation for the problem
I have one explanation for the strange behaviour.
The latter file is called by the following form in my index.php.
<?php
echo ("<form method='post'"
. "action='/codes/handlers/handle_registration.php" // called here
. "'>"
);
?>
<p>Email:
<input name="login[email]" type="text" cols="92" />
</p>
<input type="submit" value="OK" />
</form>
This action seems to make like an external environment which does not overwrite anything in the main environment.
The following picture summarizes the situation where the order of the files being called is shown.
handle_login_session.php ---- handler_registration ------
| |
index.php ----------------------------------------------------------------------->
time

Related

individual checkbox for each task returned in a ajax call [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 9 years ago.
I have a question about checkboxes. I am making a website that lets users enter their tasks for the day and can check the tasks off once they are complete. They can browse the tasks by selecting the date. I am using the AJAX get method to get the tasks from the database and display them in the webpage. I am not sure how to create a checkbox for each task I get from the AJAX method as I will be getting all the tasks for the selected date through one ajax call to the php script that connects to database and returns the tasks. And also how to identify which checkbox corresponds to which task.
In the db there are index numbers, task, date of the task, completed or not and the username
Any help will be greatly appreciated. Thanks
Make a JSON response from your PHP function that is returned with the ajax request and use the response to put the html in your page body.
For example, let's say you have the following PHP code:
<?php
$response = array();
$tasks = array(0 => "Task1", 1 => "Task1", 2 =>
"Task1");
$response['tasks'] = $tasks;
$html = '';
foreach($tasks AS $k => $v) {
$html .= '<input type="checkbox" id="task_' .$v. '" />';
}
$response['html'] = $html;
echo json_encode($response); ?>
?>
And once your AJAX request finoshes, you can put the checkboxes in your page with jQuery(?)
<script type="text/javascript">
yourAjaxFunxtinHere().success(function()
{
$('#container').html(response.html);
});
</script>

How to pass an array to a function as argument [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
I made this code where I define an array, fliepath, in which I store the locations of some files.
include 'last_file.php'; // Include the function last_file
$last_file = last_file(); // assign to the function a variable and call the function last_file
// Connect to the database
include('connect_thesis.php');
// Defining an array, which has the three paths to the three different gps receivers
$file_path[0] = "/Applications/MAMP/htdocs/php_test/check/".$last_file[0];
//echo $file_path[0]; echo "<br>";
$file_path[1] = "/Applications/MAMP/htdocs/php_test/check2/".$last_file[1];
//echo $file_path[1]; echo "<br>";
$file_path[2] = "/Applications/MAMP/htdocs/php_test/check3/".$last_file[2];
//echo $file_path[2]; echo "<br>";
Then I made a function called insert() which I want to take as input the $file_path[0]:
function insert($file_path){
$fh = fopen($file_path,'r') or die ("Could not open:".mysql_error()).......;
I call the function from the main script as:
insert($file_path[0]);
I am new in programming and I am sure somewhere I am missing something basic!
The problem is that the function doesn't run!!!
Can you help me?
Thanx
D.
I THINK I DONT PASS CORRECTLY THE VALUE TO THE FUNCTION. CAUSE I GET NOTHING AS AN ERROR!
A few points to note:
You are calling insert using only the index 0, consider using a foreach and call the function on each items in your array.
insert() -> we are missing part of the implementation, but if the file exists, you should not get an error. Keep in mind that you need to close files that you open.
or die -> it looks like you copy pasted code from elsewhere... mysql_error() will not help you much as you're dealing with files at the moment. Consider changing it to
$fh = fopen($file_path,'r') or die ("Could not open:".$file_path)
You should probably handle graciously the error instead of using "die"
I think you need this:
function insert($file_path) {
foreach ($file_path as $file) {
//Your code here
}
}

How to get latest Tweets of an User [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
I am trying to get my client latest tweets. After Searching in Google i got some code. Unfortunately the code is working in localhost, But not working in the Hosting server. Its saying couldn't find the server. I am posting the code snapshot..
<?php
function getTwitterStatus($userid){
$url = "http://twitter.com/statuses/user_timeline/$userid.xml?count=1";
$xml = simplexml_load_file($url) or die("could not connect");
foreach($xml->status as $status){
$text = $status->text;
}
echo $text;
}
//my user id AmitEducation
getTwitterStatus("AmitEducation");
?>
Please help me out. If anyone have better suggestion please help me.
Use the Twitter Search API, it's really awesome. And it's JSON. [+1]
$tweets = json_decode(file_get_contents("http://search.twitter.com/search.json?q=php&rpp=5&include_entities=true&result_type=mixed"));
foreach($tweets->results as $t){
echo "Username: {$t->from_user_name}";
echo "Tweet: {$t->text}" . PHP_EOL;
}
Please read the documentation for how to use and see examples.
change the username in the link
<?php
$json = file_get_contents("http://twitter.com/status/user_timeline/username.json?count=10", true); //getting the file content
$decode = json_decode($json, true); //getting the file content as array
print_r($decode);
?>

add a required field with php [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 9 years ago.
I want to design a dynamic form using php. suppose i have created a text box using javascript as follow
var labels=new Array()
function add(type) {
var element = document.createElement("input");
var label=prompt("Enter the name for lable","");
labels.push(label);
document.getElementById('raj').innerHTML=document.getElementById('raj').innerHTML+label;
element.setAttribute("type", type);
element.setAttribute("name",label);
var rohit = document.getElementById("raj");
rohit.appendChild(element);
document.getElementById('raj').innerHTML=document.getElementById('raj').innerHTML+"<br/>";
}
I want to make this field as a required field using php(it is required). what should i do for that
In html5 you can set a required attribute.
element.setAttribute("required", "required");
If you want to use PHP, as mentioned in the question, that would happen on the server side. You have to validate the request received by a php script & check whether it is empty or not.
It's not clear what you want do do, but if you're using POST to send it to PHP then you can check if the value is empty like this
<?php
if(empty($_POST['value_name']))
{
echo 'value is required';
}
else
{
// OK
}

Automatic logged out [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
I have a php page from where my users log-in to the application. It is working fine.
Yesterday, all of a sudden the users were able to login but were forced out and redirected to the login page again.
My database has logged in the user's login timings and this problem was automatically solved after about 2 hours.
Why will like this happen?
In the following code it will check for the session value and if it is not found then redirect to the error page.
Yesterday, it was redirecting to error page even if the session value was set.
<?php
if($_SESSION['ucd']<>"" && $_SESSION['sid']<>"" && $_SESSION['sid']<>0)
{
$query="select count(*) from active_sessions where user_cd='".$_SESSION['ucd']."'
and session_no='".$_SESSION['sid']."' and START_TM like DATE_FORMAT(now(),'%Y-%m-%d%')";
//echo $query;
$cnt=$dbop->select($query);
if($cnt[0] == '0')
{
$sender = "sender=".urlencode($_SERVER['PHP_SELF']);
session_unset();
header("Location:../login/error.html?$sender");
die;
}
else{
$query = "update active_sessions set LAST_ACTIVITY = NOW() WHERE SESSION_NO = ".$_SESSION['sid'];
mysql_query($query);
?>
<?php
}
}
else
{
$sender = "sender=".urlencode($_SERVER['PHP_SELF']);
session_unset();
header("Location:../login/error.html?$sender");
die;
}
?>
I don't see session_start() anywhere in your code.

Categories