make a session user have a automatic code input - php

when a user logs in, I want a input type text to be already filled with some pre-determined letter...
how can I make that possible?

You can do it in two ways:-
You can give control from the Admin area, so that the Administrator has the full control. This can be possible by providing a list drop down with all the recommended alphabets, in the Settings section of your Admin area, and let the Admin choose her preferred alphabet & store it in the database. Then you can retrieve it from the database & show it in the front-end when any user logs in.
When the user logs in, you can generate a random letter, using the PHP function "rand()".
One simple code snippet for the second solution will be:-
<?php
// This function considers both the cases for each of the letters.
function randLetter() {
$int = rand(0, 51);
$a_z = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
$rand_letter = $a_z[$int];
return $rand_letter;
}
/**
* This function considers only one specific cases for each of the letters.
*/
function randSpecificCaseLetter($lower_case = TRUE) {
if ($lower_case) {
$letter = chr(rand(97, 122));
}
else {
$letter = chr(rand(65, 90));
}
return $letter;
}
?>
Hope it helps.

Related

how to track goal points?

I am having a goal for the user that needs to hit and that user passed the goal I want it to show a different color. So right now I have total goals needed by having goals_needed * time_spent / day_length.
For example if the goal is 10, and user gets 11 points I want it to show a different color because he passed the goal. How would I get the result to show passed goal ?
I was thinking something like this
$color_performace = function($value, $goal) {
if($value < $goal)
return 'notenough';
elseif($value >$goal)
return 'awesome';
else
return 'enough';
};
and function
$total_goals_class = $color_performance($total_goals,round($goals_needed*$goal_multiplier));
It is not clear exactly what you are after but if you want to deal with getting things on the page to change colour you will need to output html & css.
So perhaps have something like:
$color_performace = function($value, $goal) {
if($value < $goal)
return '<p style="color: red;">notenough</p>';
elseif($value >$goal)
return '<p style="color: green;">awesome</p>';
else
return '<p style="color: yellow;">enough</p>';
};
That is a quick and dirty example to show the concept, I would probably change it to use classes and have the css set in a main file somewhere, and use hex codes for colours.

Hide the Fields audited list in sugarcrm to the regular user

How can we hide the list of fields audited to the regular user,but to admin the list of audited fields are need to visible,
the following line need to hide for the regular user and only admin be able to see the below line
Fields audited in this module: name, title, etc
I am using sugarcrm ce 6.5.x
I'm understanding your question as "when a Regular User views the Audit history, he or she should not see the list of fields that are audited. System Administrators can still see this list. All Users should still see the actual audit/history table." If that's correct, here is my advice:
The list (and popup itself) is handled and generated in modules/Audit/Popup_picker.php so you would start by reviewing the code there.
It seems to me that the two lines below are responsible for displaying this output:
echo $start_tag.translate('LBL_AUDITED_FIELDS', 'Audit').$fields.$end_tag; (on/around line 139)
echo $start_tag.translate('LBL_AUDITED_FIELDS', 'Audit').$end_tag; (on/around line 143)
With that code found, I would copy the file modules/Audit/Popup_picker.php to custom/modules/Audit/Popup_picker.php and make adjustments to add a check like if(is_admin($GLOBALS['current_user']) into the code. If you used this as an extra condition, you might get something like the following, note that my changes are annotated by a <-- in the PHP comment, and that I cleaned up some indentation and white space):
if($field_count > 0 && is_admin($GLOBALS['current_user'])) // <-- Added Admin-Check Condition
{
$index = 0;
foreach($audited_fields as $key=>$value)
{
$index++;
$vname = '';
if(isset($value['vname']))
$vname = $value['vname'];
else if(isset($value['label']))
$vname = $value['label'];
$fields .= str_replace(':', '', translate($vname, $focus->module_dir));
if($index < $field_count)
{
$fields .= ", ";
}
}
echo $start_tag.translate('LBL_AUDITED_FIELDS', 'Audit').$fields.$end_tag;
} elseif(is_admin($GLOBALS['current_user'])) { // <-- changed ELSE to ELSEIF and added Admin-Check Condition
echo $start_tag.translate('LBL_AUDITED_FIELDS', 'Audit').$end_tag;
} else { // added new ELSE statement
echo $start_tag.$end_tag; // <-- Regular users shouldn't see the message at all so render the empty table
}
I haven't ran and tested this code but I expect that this will give you a good strategy to dig in and create the customization yourself.

Basic addition for form security gives wrong answer PHP

I have a basic math question that the user has to answer before they can send an email:
$first_num = rand(1, 4);
$second_num = rand(1, 4);
$send = #$_POST['send'];
if($send){
//The user's answer from the input box
$answer = #$_POST["answer"];
if($answer == $first_num + $second_num) {
//Do stuff
}
else {
$error_message = "You answered the question wrong!";
}
}
I answer the question correctly (unless my first grade math is off!) yet it says I have the question wrong. I am not sure what the issue is but I imagine it is something to do with the fact that php executes immediately when the page is loaded and so new numbers are generated as soon as the user presses the submit button? Or am I way off? If that is the case, what can be done to solve this?
The problem is that you are setting your values every time your script is called. So when you post your form, two new values are set and they are likely not the same values as when you called the script the first time to show the form.
You should store your variables in a session and retrieve these values when you process your post request.
Something like:
session_start();
if (!isset($_SESSION['numbers']))
{
$_SESSION['numbers']['first'] = rand(1, 4);
$_SESSION['numbers']['second'] = rand(1, 4);
}
...
if ($answer == $_SESSION['numbers']['first'] + $_SESSION['numbers']['second']) {
//Do stuff
/**
unset the variables after successfully processing the form so that
you will get new ones next time you open the form
*/
unset($_SESSION['numbers']);
...
Note that you will need to use the session variables everywhere where you are using your own variables right now.
You should include the two numbers as hidden form inputs in your HTML, and then do the math with the entries in your $_POST array.
Make your code start like this instead:
$first_num = $_POST["first_num"];
$second_num = $_POST["second_num"];

Random SQL record while excluding specific records

I have a CodeIgniter PHP application that shows two movie covers. Beside them is a "random movie" button that uses AJAX to replace the two movies with a new set of movies. You can continue to click this, over and over, and see it continue to replace the images of the movie covers. The first two covers to show are set as the defaults, and they should never show after the user has clicked the random movie button. The problem is this: When clicking the random movie button, it will some times take many clicks to finally show a new cover. That is, the same cover will be returned multiple times in a row. The two different covers being fetched are being called from slightly different URLs, so they will rarely both break at the same time. This lets me know that it is refreshing, but that the function is returning the same movie multiple times. If I access the url that is being called via AJAX directly, I never see this take place since I have used the Session class to store the last movie's and exclude it from the SQL query (i.e. WHERE id NOT IN ($default_movie, $last_movie)). Any idea why accessing the url directly would work fine, but when calling via AJAX, I'm seeing this behavior?
I know this may not have been as clear as possible, so let me know if I can clarify something that doesn't make sense. I'll add code if that helps as well. Thanks friends!
Query to get random movie:
SELECT * FROM (`movies`) WHERE `id` NOT IN (2, 10) ORDER BY RAND() LIMIT 1
Model method:
public function getRandom($count = 1, $featured = FALSE, $series = FALSE, $exclude = 0, $last = 0) {
$this->db->order_by('id', 'random');
$this->db->limit(1);
$conditions = array();
if ($exclude > 0) {
$conditions['id !='] = $exclude;
}
if ($last > 0) {
if (!empty($conditions['id !='])) {
$conditionsNotIn = "id NOT IN (" . $conditions['id !=']. ", $last)";
unset($conditions['id !=']);
$this->db->where($conditionsNotIn);
} else {
$conditions['id !='] = $last;
}
}
if ($featured) {
$conditions['featured'] = 1;
}
if ($series) {
$conditions['current_series'] = 1;
}
$movie = $this->db->get_where('movies', $conditions);
$movie = $movie->row();
if (!is_null($movie)) {
return $movie;
} else {
return FALSE;
}
}
Any idea why accessing the url directly would work fine, but when
calling via AJAX, I'm seeing this behavior?
I have an idea yes.
Browser caching.. PITA!
Try turning off caching explicitly:
$.ajaxSetup({cache: false});
Put that before your ajax request, assuming you're using jQuery.
If you're not you need to append some random variable to the url, this keep the browser from caching the requests.

PHP if logic returns one result on first run and different results on all subsequent runs

I'm having a little issue with some php logic in my main index page, that will include certain pages based on the results of some functions, mainly to do with login/logout and the first time a user logs in after registering. The php that manages the includes is below:
UPDATE: (Based on suggestions from #arjan and #bigman I've updated the code as follows. The end result is still the same).
<?php
if ($login->checkForRegisterPage()) {
include("views/pages/home.php");
// are we logged in ?
} elseif ($login->isLoggedIn()) {
// check whether account is activated
if (!$login->checkActivated()) {
include("views/pages/activate.php");
// check whether user has logged in before
} elseif ($login->checkFirstLogin()) {
include("views/pages/build_profile.php");
// check action in URL and redirect accordingly
} elseif ($checkaction->checkForBuildProfilePage()) {
include("views/pages/build_profile.php");
} elseif ($checkaction->checkForViewProfilePage()) {
include("views/pages/profile.php");
// if all else fails, load the dashboard
} else {
include("views/pages/dashboard.php");
}
} else {
// not logged in, showing the login form
include("views/pages/home.php");
}
?>
The problem is with the two functions $login->checkActivated(); and $login->checkFirstLogin(); included below:
public function checkFirstLogin() {
$checkfirstlogin = $this->db->query("SELECT first_login FROM users WHERE first_login = 'Y' AND user_name = '".$this->user_name."';");
if($checkfirstlogin->num_rows == 1) {
return true;
} else {
return false;
}
}
public function checkActivated() {
$checkactivated = $this->db->query("SELECT activated FROM users WHERE activated = 'N' AND user_name = '".$this->user_name."';");
if($checkactivated->num_rows == 1) {
return false;
} else {
return true;
}
}
When the user first logs in, these functions return the correct result and I receive the page that I want. However, after login, I can still click and travel to other links on the page e.g. checkForViewProfilePage(); looks for view=profile in the URL. The thing is in order for the logic to reach the point where it even checks for that, it would have had to get past the two functions checkActivate(); and checkFirstLogin();, which it shouldn't be able to do while those criteria are met, but it still can. I hope I'm making sense. Can anyone see an error?
Obviously my login form calls the Login class which the awkward functions are stored in, and so this would be loaded on login, but the class is included in the same way here so I'm not sure why the functions don't appear to be firing.
you can use elseif to stop the nesting from going deeper.
also, why call every function twice? Only once in the if should be enough.
I solved it. The problem was higher up in the Login class - in Login db connections are only created when a session_start() is fired, i.e. on first login. Created a new class with db connection launched with each function and everything worked as it was.
Thanks to #arjan and #bigman for the formatting tips r.e. nesting.

Categories