Jump to content

[SOLVED] news and login


winder

Recommended Posts

hello all :)

 

i need a bit of help.

 

i made a site in which every page it has a login form. The connection with the database and the validation of the username and the password is done in a separate file (checklogin.php).  So in each login form, in every page i post the data to checklogin.php, and then redirecting back in the page which contains the form.

 

The home page has a sidebar containing news titles, and every title is a link to a page that elaborates the selected news. I made the title link passing a different value in a variable $id. So the page that elaborates  the news GETs the id value and shows the correct news description.

 

The problem is that when i click the login button in the form placed in the page that elaborates the news, when redirecting back there is no id value passed back. So no news shows up.

 

I want to make the page somehow remember the id value that was passed from the link in homepage, when redirecting back from checklogin.php.

 

sth like a global variable?

 

sorry but i'm new in php.

 

thanks in advance!! 

Link to comment
Share on other sites

yes i know about session variables. And the login works as you describe. But i don't know how this can help.

 

maybe i wasn't so clear.

 

the problem is the redirection. After the redirection the news page loads, but $id has no value. that's why it doesn't know what news id to show. The home page, (the news titles links) set the values of the $id.

Link to comment
Share on other sites

You create session variables for all of your $id's that you want displayed that are dynamic. So a person logs in and visits the news page, the session variables are set, and no matter what page they goto forward or back the session is already started to it will stay the same.

Link to comment
Share on other sites

You would do it on the page, so if someone went to index.php you would do:

 

<?php
session_start();

$_SESSION['id']=$link;

echo "$_SESSION['id']";
?>

 

Then for like news.php:

 

<?php

session_start();

echo "$_SESSION['id']";

?>

 

This will both echo the same result of the link, so you will just need to populate the session variable with the same thing that you want the link to contain.

Link to comment
Share on other sites

i'm not quite sure that this is the case...

 

i'll try to explain bettter...

 

the thing is that the $id is used in a query to find the news id and show only the 3 latest news. after that each of the 3 links i have in index.php for different news, set the id value to the news page and again $id is used by the news page in a query in order to show only the description of the news with the id which was passed from index.php.

 

this works perfectly except when hitting the login button, in the form placed in the news page, cause login goes to checklogin.php, and redirects back, and that's why news page doesn't know what news id to show.

Link to comment
Share on other sites

It will work just as intended. You just need to set your session variables at the first instance the id appears, which sounds like index.php, then no matter what page they goto it is already set in a variable that you can use to print the result. So in index.php you create a session variable that is the output for the link, based on your query, you will never have to query again for that session to get the result because it is stored in a variable that you can print on any page.

Link to comment
Share on other sites

i just can't get you  ::)  but thank you for your effort!

 

this is the code that generates each news block, in index.php.

 

<?php
include("gr/database.php");
mysql_query("SET NAMES 'utf8'");
$result = mysql_query ("SELECT * FROM news");
while($row = mysql_fetch_object($result)) 
   {
  ?>
  <dt><a href="gr/news.php?id=<?php print "$row->ID"; ?>"><?php print "$row->STITLE" ?></a></dt>
  <dd><a href="gr/news.php?id=<?php print "$row->ID"; ?>"><img id="mainimg" src="images/news/<?php print "$row->IMAGE" ?>" /></a><?php print "$row->SDES" ?></dd>
  <?php
   }
   mysql_free_result($result);
   $c=mysql_close();
  ?>

 

and this is what news.php page does...

 

<?php
include("database.php");
$id = $_GET['id'];
mysql_query("SET NAMES 'utf8'");
$result = mysql_query ("SELECT * FROM news WHERE news.ID='$id'");
$row = mysql_fetch_object($result);
  ?>
  <div class="item"><p><img src="../images/news/<?php print "$row->IMAGE"; ?>" /><span class="title"><?php print "$row->TITLE";?> </span><?php print "$row->DESCRIPTION"; ?></p></div>
  <?php 
  mysql_free_result($result);
  $c=mysql_close();
  ?>

Link to comment
Share on other sites

Can i also see your login script/form? I now fully understand what your issue is...one way of doing it is to make this into a seperate file and include it, then in your news.php file you check for the variable passed and if it is not passed then you include the same file but do not echo the results you would want from index.php, you can do that by checking what file it is included in with the $_SERVER['PHP_SELF'] global variable.

Link to comment
Share on other sites

i don't understand what you instruct me to do :) i'll read your post again and again till i'll understand :) anywat... here is the code for the login. Maybe you can add some code in the one i provided, to understand what you mean.

 

checklogin.php

 

<?php
session_start(); 

include("database.php");

$username = $_POST['username'];
$password = $_POST['pass'];

$q=mysql_query("SELECT * FROM reg_cust WHERE reg_cust.USERNAME='$username' and reg_cust.PASSWORD='$password'");

$count=mysql_num_rows($q);

if($count==1){
$_SESSION['username'] = $_POST['username'];
$_SESSION['password'] = $_POST['pass'];
$_SESSION['loged_in'] = TRUE;
header("Location: ".$_SESSION['pageuri']);
}
else {
$_SESSION['loged_in'] = FALSE;
header("Location: ".$_SESSION['pageuri']);
}
?>

 

and the login form inside news.php... (some words are in greek but you get the general idea)

 

<?php
  $_SESSION['pageuri']= "news.php"; 
  if(isset($_SESSION['loged_in'])){
   if($_SESSION['loged_in']){
   $uname = $_SESSION['username'];
   ?>
   <h4>Είσοδος Μελών</h4>
   <div id=succes><p><img src="../images/tick.png" />Καλώς ήρθες<span><?php print $uname; ?></span></p></div>
   <p><a href="logout.php">Έξοδος</a> | <a href="register.php">Εγγραφή</a></p>
   <?php
   }//end if
   else{
   unset($_SESSION['loged_in']);
  ?>
  <h4>Είσοδος Μελών</h4>
  <div id=error><p><img src="../images/attention.png" />Λανθασμένα Στοιχεία Χρήστη</p></div>
  <form name="loginform" id="login" method="post" action="checklogin.php">
  	<fieldset>
<label for="username"><span>Όνομα Χρήστη</span><input type="text" name="username" id="username" maxlength="20"></label>
<label for="pass"><span>Κωδικός Πρόσβασης</span><input type="password" name="pass" id="pass" maxlength="12">
</label>
</fieldset>
  </form>
  <p><a href="javascript:login()">Είσοδος</a> | <a href="register.php">Εγγραφή</a></p>
  <?php 
  	}//end else
  }//end if
  else{
  ?>
  <h4>Είσοδος Μελών</h4>
  <form name="loginform" id="login" method="post" action="checklogin.php">
  	<fieldset>
<label for="username"><span>Όνομα Χρήστη</span><input type="text" name="username" id="username" maxlength="20"></label>
<label for="pass"><span>Κωδικός Πρόσβασης</span><input type="password" name="pass" id="pass" maxlength="12">
</label>
</fieldset>
  </form>
  <p><a href="javascript:login()">Είσοδος</a> | <a href="register.php">Εγγραφή</a></p>
  <?php
  }//end else
  ?>

 

 

 

 

Link to comment
Share on other sites

ok.. but i only know that in links you can set variables like this...?id=3

 

how can i set session variables when a link is pressed? sth like ?session['id']=3;  ;D?

 

hmm

$_SESSION['id']= $_GET['id'];

is that what you mean set the value of session using the url

 

 

Link to comment
Share on other sites

oh finally solved it!! thank you guys for your tips and effort!!

 

teng84 that's what i was looking for!! thanks :) It's so obvious that i don't know how i couldn't think of it.

 

now i'm using this little code... and everything works ok!!

 

if (isset($_GET['id'])){
$_SESSION['newsid'] = $_GET['id'];
}
$id = $_SESSION['newsid'];

 

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.