Album coding errors, simple PHP - php

I'm currently trying to develop the following function:
When someone wants to see their album or profile photos, I would like to use either album_id or id in the url, like this:
album.php?album_id=(numeric Number)
album.php?id=(numeric number)
I am currently able to execute the first command and when I attempt the second, it fails to load instead I'm taken to the index page (as per the script's design.)
Here is my code:
$album_id = addslashes(is_numeric($HTTP_GET_VARS["album_id"]));
$profile_id = addslashes(is_numeric($HTTP_GET_VARS["id"]));
?>
<?php
if($album_id==Null||!$profile_id==Null)
{
print("<script language='JavaScript'> window.location='index.php'; </script>");
}
else
{
if ($album_id==$album_id)
{
include("/home/emo/public_html/incldues/layout/photos/pages/albums/album_photos.php");
}
else
{
if ($profile_id==$profile_id)
{
include("/home/emo/public_html/incldues/layout/photos/pages/albums/profile_photos.php");
}
else
{
include("/home/emo/public_html/incldues/layout/photos/pages/albums/album_photos.php");
}
}
}
?>
And to clarify, those "pages" contain nothing but bold saying:
Show album photos and Show user photos
But what I can't seem to fathom as to what my problem might be.

If you call album.php?album_id={id}, $_GET['id'] is null, making your first if statement return to the index page.
if (($album_id == null) || ($profile_id == null)) { ... }
One will always be null unless you call album.php?album_id={aid}&id={pid}
if (($album_id == null) && ($profile_id == null)) {
# redirect to index.php
} else {
if ($album_id != null) {
# load album
} else if ($profile_id != null) {
# load profile
} else {
# both given, redirect to error.
}
}

Your condition if($album_id==$album_id) will always be true because they both are same and the else part is never executed.

Related

PHP - Test if $_GET value is one of the following

First ever question on stackoverflow (new user) so bare with me.
I have a website www.website.com/?view=overview .
I want to test if VIEW exists, if it does not then I redirect to another page
(this is done with empty($_GET['view') and works
If that test passes, I now want to get that view is one of 5 values, if it is then I allow the user to continue if not I redirect to another page.
I have tried with a variable and array, with in_array and the most I can get it to do is properly recognize a single value. When I try to test for more than one, it always redirects. Here is the example if I am testing for two values (overview and settings) but again the total number tested should be 5.
if (empty($_GET['view'])) {
header("Location: www.website.com/?view=overview");
} else {
if (($_GET['view'] != "overview") || ($_GET['view'] != "settings")) {
header("Location: www.website.com/?view=overview");
}
}
}
I searched everywhere and I am stuck!
You're using || (OR) when you should be using && (AND)
if (empty($_GET['view'])) {
header("Location: www.website.com/?view=overview");
} else {
if (($_GET['view'] != "overview") && ($_GET['view'] != "settings")) {
header("Location: www.website.com/?view=overview");
}
}
}
If $_GET['view'] equals 'settings' then it's not equal to 'overview', that part of the condition will pass and your code will redirect. And vice versa.
You can use in_array(needle, haystack) having a list of valid values:
$valid_views = ['overview', 'settings'];
if (!in_array($_GET['view'], $valid_views){
header("Location: www.website.com/?view=overview");
} else {
// view is ok, continue
header("Location: www.website.com/?view=".$_GET['view']);
}

Redirect user after return false

I have some validation fields like this :
if (empty($_POST['finalidade_imob']) || $finalidade_imob == "Escolha 1" ||
!in_array($_POST['finalidade_imob'], $fi_options)) {
$finalidade_imob = "Escolha 1";
$error++;
} else {
$finalidade_imob = $_POST['finalidade_imob'];
}
//on the end
if ($error !== 0) {
return false;
}
But the location of my form is on bottom of my page, so when return false is called the page is reloaded on the top.
If i put this:
if ($error !== 0) {
header('location:perfil.php#content_cadastro_completo');
return false;
}
This page is reloaded for exactly to the place i need, but return false dont work... I know why return dont work, i put that for explain what i need...
So what i need do to goal this target?
Sorry my bad english...
Edit Without js... Only PHP.
My question no clear for this solution, but in my all context this work.
The solution that I found is put placeholder in my action.
action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>#content_cadastro_c‌​ompleto"
and
if($error !== 0)
{ return false;//no need header() to go to the placeholder...
}

Incapable of catching error in if clause (updated with answer)

In this code below, $alb->error has an error message in it. I can verify this with echo, no problem.
The issue arises when I want to catch that error in an if clause. First, the code:
if($action == "add")
{
//PUT CODE TO SAVE A NEW ALBUM HERE
//SHOULD RETURN ALBUM ID
$alb = new album();
//fill object album with properties to be saved
$alb->date = checkNull($_POST['alb_date']);
$alb->name = checkNull($_POST['alb_name']);
$alb->desc = checkNull($_POST['alb_desc']);
$alb->cat_id = $_POST['categories'];
//call save method of album
$alb->create_album();
//catch error
if($alb->error <> "")
{
header("Location: add_album.php?error='$alb->error'");
}
//set album id to pass it along to add_pictures
$alb_id = $alb->id;
}
elseif($action == "edit")
{
//PUT CODE TO SAVE EDITED ALBUM HERE
//SHOULD RETURN ALBUM ID
}
header("Location: add_pictures.html?alb_id='$alb_id'");
After $alb->create_album(), $alb's error property is filled by omitting a required field in the input form. This is what I want. But then, I need to catch that error, and redirect the user to the previous page, where the error will be displayed.
Here's what I've tried so far:
//catch error
if(isset($alb->error))
{
header("Location: add_album.php?error='$alb->error'");
}
Didn't work.
//catch error
if(!is_null($alb->error))
{
header("Location: add_album.php?error='$alb->error'");
}
No cigar.
//catch error
if($alb->error <> "")
{
header("Location: add_album.php?error='$alb->error'");
}
still, zero joy.
The if clause never gets processed. What am I missing?
Thank you for your time.
So the problem was that I had a header redirect after my outer if clause for the type of action: if action=add or edit. When I wrapped it in an else clause, it worked:
}
elseif($action == "edit")
{
//PUT CODE TO SAVE EDITED ALBUM HERE
//SHOULD RETURN ALBUM ID
}
else
{
header("Location: add_pictures.html?alb_id='$alb_id'");
}
Thanks for your help!!

Function parameters when variables values are not set

How can I call a function without throwing any errors when certain variables are not set?
For example, I need to logUserActivity($uName,$uId). This'll work ok if a user is logged in and both those variables are set. However, it will not work if a user is not logged in, and will throw an error.
In that case can, something be done to make those two values NULL?
I did this: logUserActivity($uName=NULL,$uId=NULL) but this makes them forever NULL.
I also did function logUserActivity($uName=NULL, $uId=NULL), by this still throws an error.
You have to set default value of a function to make it optional
Like try this
function logUserActivity($uName='', $uId='')
function logUserActivity($uName=NULL, $uId=NULL){
if($uName != ''){
echo 'Uname is set - '.$uName;
}
if($uId != ''){
echo 'uId is set - '.$uId;
}
}
logUserActivity(NULL,20);//O/p - uId is set - 20
logUserActivity('Test',NULL);//O/p - Uname is set - Test
logUserActivity('Test',20);//O/p - uId is set - 20,uId is set - 20
logUserActivity(NULL,NULL);//NO O/p
Write like this:
function logUserActivity($uName=NULL, $uId=NULL) // If nothing is passed it will take it as null
{
if($uName==NULL && $uId == NULL) {
//User is not logged in
}
else
{
//user is logged in
}
}
And Call it as when user is logged in
logUserActivity("userName", 12345);
And When user is not logged in
logUserActivity();
For more information you can check this out:http://us2.php.net/manual/en/functions.arguments.php
function logUserActivity($uName=null,$uId=null) {
if ( !isset($uName) || !$uName ) {
$uName = 'some default'; // optionally, return; could be sued
}
if ( !isset($uId) || !$uId) {
$uId = 'some default'; // optionally, return; could be sued
}
}

PHP function to check if 2 variables are empty

I have an index page, I want it to include a page called splash.php and not display.php when a user lands on index.php, but once a user does something (sets a variable) ie if a user searches (variable "query") i want it to include display.php and not include splash.php
What is wrong with this code?
function hasGet()
{
return !empty($_GET['fact']);
return !empty($_POST['query']);
}
if (hasGet()) {
include("display.php");
}
else {
include("splash.php");
}
This question should be removed
Only the first return statement is executed. Try:
return !empty($_GET['fact']) && !empty($_POST['query']);
A better way to accomplish what you are trying to do is use sessions.
index.php
<?php
session_start();
if (!isset($_SESSION['visited'])) {
$_SESSION['visited'] = true;
include 'splash.php';
} else {
include 'display.php';
}
?>
This way after a user visits index.php for the first time, $_SESSION['visited'] is set to true and it won't show the splash page throughout their visit.
You cannot have two returns as you are doing. Try
return (!empty($_GET['fact']) && !empty($_GET['query']));
You might want to try this...
if($_SERVER["SCRIPT_NAME"] == "/index.php"){
include("splash.php");
}else{
include("display.php");
}
2.
if(!empty($_GET["fact"]) || !empty($_POST["query"])){
include("display.php");
}else{
include("splash.php");
}

Categories